以我viewDidLoad中予編程方式創建一個UIButton併爲其分配標籤1如何在UIButton創建後改變框架?
後來我使用的代碼: 的UIButton * otherButton = [self.view viewWithTag:1]; 現在這個按鈕完美的與代碼,改變框架等! 除非我得到警告: 不兼容的指針類型從「的UIView *」
分配給「的UIButton *」有沒有更好的方式找到按鈕,或者我應該只是忽略這個錯誤嗎?
以我viewDidLoad中予編程方式創建一個UIButton併爲其分配標籤1如何在UIButton創建後改變框架?
後來我使用的代碼: 的UIButton * otherButton = [self.view viewWithTag:1]; 現在這個按鈕完美的與代碼,改變框架等! 除非我得到警告: 不兼容的指針類型從「的UIView *」
分配給「的UIButton *」有沒有更好的方式找到按鈕,或者我應該只是忽略這個錯誤嗎?
您可以只投它:
UIButton* myButton = (UIButton*)[self.view viewWithTag:1];`
你也可以多一點的防守和檢查:
UIView* v = [self.view viewWithTag:1];
UIButton* myButton = [v isKindOfClass: [UIButton class]] ? (UIButton*)v : nil;
NSAssert(myButton == v, @"View with tag 1 was not a UIButton");
你必須明確地投下UIView
成UIButton
這樣。
UIButton* otherButton = (UIButton *)[self.view viewWithTag:1];
問題的標題和內容無關。 – wcd