在我的ViewDidLoad中,我以编程方式创建一个UIButton,并为其分配标签1.
稍后我使用代码: UIButton * otherButton = [self.view viewWithTag:1]; 现在这个按钮与代码完美配合,改变框架等! 除了我得到警告: 不兼容的指针类型分配给' UIButton *'来自' UIView *'
有没有更好的方法来找到按钮,或者我应该忽略错误?
答案 0 :(得分:0)
你可以施展它:
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");
答案 1 :(得分:0)
您必须明确地将UIView
投射到这样的UIButton
。
UIButton* otherButton = (UIButton *)[self.view viewWithTag:1];