2011-06-09 30 views
0

对不起,如果它被问到某处,但作为初学者,我需要一个非常具体的答案为我的问题。错在哪里,更正和建议。我试图分开alloc和initWithRed ...消息,但它似乎不工作

我写那些正在申请didFinishLaunchingWithOption:

UIColor *myBackgroundColor = [[UIColor alloc]initWithRed:.87 green:.77 blue:.56 alpha:.99]; 
[window setBackgroundColor:myBackgroundColor]; 

它的工作,并更改背景颜色,然后我尝试这两个信息中分离出来。

UIColor *myBackgroundColor = [UIColor alloc]; 
[myBackgroundColor initWithRed:.87 green:.77 blue:.56 alpha:.99] 
[window setBackgroundColor:myBackgroundColor]; 

我该如何编码才能使其正确运行?我将需要理由和更正。非常感谢。

+0

更多的问题。如果我需要写“myBackgroundColor = [myBackgroundColor initWithRed:.87 green:.77 blue:.56 alpha:.99]”为什么会“[myUIView initWithFrame:myCGRect];”工作好吗? – 2011-06-09 15:07:13

+1

如果您还有其他问题,应该将其作为新问题而不是评论。 – 2011-06-09 15:19:32

回答

3

你不能假设allocinit具有相同的返回值。

下面应该工作:

UIColor *myBackgroundColor = [UIColor alloc]; 
myBackgroundColor = [myBackgroundColor initWithRed:.87 green:.77 blue:.56 alpha:.99] 
[window setBackgroundColor:myBackgroundColor]; 

我不明白你为什么会想,虽然添加额外的线。

+0

理解,非常感谢!只是为了更清楚地了解它。 – 2011-06-09 15:05:20

+0

@Shane Hsu,我看到你是新人,所以我只想告诉你,你应该接受最能帮助你的答案。您可以通过点击答案旁边的绿色复选标记来完成。 – 2011-06-09 15:08:47

+0

非常感谢,这是一个很好的答案。 – 2011-06-09 15:12:18

0

使用...

[UIColor colorWithRed:0.87 green:0.77 blue:0.56 alpha:0.99]; 
+1

坚实的建议,但不是问题的答案。 – 2011-06-09 15:02:11