2011-06-09 18 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

相關問題