2011-04-03 121 views
1

我想將suitSize分配給scrollButton我做錯了什麼?作爲賦值左操作數所需的左值

UIView *scrollButton = [suitScrollView viewWithTag:1]; 
CGSize suitSize =CGSizeMake(10.0f,10.0f); 
(UIButton *)scrollButton.frame.size=suitSize; 

回答

5

框架是屬性,而不是結構字段。你不能分配給它的一個子域。把它看作一個函數調用;屬性的點語法很方便。

此:

scrollButton.frame.size = suitSize; 

等同於:

[scrollButton frame].size = suitSize; 

不工作;分配給函數結果的字段沒有任何意義。

相反,這樣做:

CGFrame theFrame = [scrollButton frame]; 
theFrame.size = suitSize; 
[scrollButton setFrame: theFrame]; 

或者,如果你喜歡:

CGFrame theFrame = scrollButton.frame; 
theFrame.size = suitSize; 
scrollButton.frame = theFrame; 

注意,鑄造scrollButton到一個UIButton是沒有必要的; UIViews也有框架。

+0

謝謝它的工作原理 – Anton 2011-04-03 06:18:34

1

不要混合賦值左側的屬性訪問器和結構體字段訪問。

左值是可以出現在賦值左側的表達式。當你混合使用屬性時,結果表達式不是左值,所以你不能在賦值的左邊使用它。

(UIButton *)scrollButton.frame.size=suitSize; 

scrollButton.frame部分爲屬性訪問。 .size部分訪問frame結構的字段。上面的Steven Fisher的例子是分解代碼以避免問題的正確方法。

0

當是這種方式,你不能直接設置子結構結構性能處理...

(UIButton *)scrollButton.frame.size=suitSize; 

中的UIButton的框架屬性是一個的CGRect結構。編譯器會看到您的.size訪問權限,並嘗試將其解析爲不存在的setter。相反,混合不同,需要處理的CGRect結構類型的整體屬性訪問結構成員的訪問...的

CGRect frame = (UIButton *)scrollButton.frame; 
frame.size = CGSizeMake(100, 100); 
(UIButton *)scrollButton.frame = frame; 
相關問題