【问题标题】:How to change the frame a UIButton after it has been created?UIButton 创建后如何更改框架?
【发布时间】:2015-01-17 16:24:14
【问题描述】:

在我的 ViewDidLoad 中,我以编程方式创建了一个 UIButton 并为其分配标签 1。

稍后我使用代码: UIButton* otherButton = [self.view viewWithTag:1]; 现在这个按钮可以完美地与代码、改变框架等一起工作! 除了我收到警告: 从“UIView *”分配给“UIButton *”的指针类型不兼容

有没有更好的方法来找到按钮,或者我应该忽略错误?

【问题讨论】:

  • 你问题的标题和内容无关。

标签: ios objective-c uibutton


【解决方案1】:

你可以直接施放它:

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");

【讨论】:

    【解决方案2】:

    您必须像这样将UIView 显式转换为UIButton

     UIButton* otherButton = (UIButton *)[self.view viewWithTag:1]; 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多