【问题标题】:Accessing sender methods from another class从另一个类访问发送方方法
【发布时间】:2012-03-19 12:44:58
【问题描述】:

我有 2 个班级,ClassA 和 ClassB

ClassA 调用 ClassB 的方法:- (void)someMethod:(id)sender

在上述方法中,我需要从 ClassB 中获取发送者(发送者是 UIView 的一个子类,上面有一个 UIButton)并使用类似于以下内容的内容更新按钮图像 [发送者 updateButtonImageWithImage:image]

(UIView子类有方法:- (void)updateButtonImageWithImage:(UIImage *))

但是每当我从 ClassB 调用它时,应用程序都会崩溃 [UIButton updateButtonImageWithImage:] 无法识别的选择器等等等等。

我做错了什么?

【问题讨论】:

    标签: objective-c ios uiview uibutton unrecognized-selector


    【解决方案1】:

    发送者是 UIView 的子类,上面有一个 UIButton

    这是不正确的——如果按钮是触发-someMethod:的东西,那么按钮,而不是包含它的视图,是传递给-someMethod:sender。最简单的方法是获取按钮的超级视图(应该是您的视图子类),然后调用您的-updateButtonImageWithImage:

    [(MyViewSubclass *)[sender superview] updateButtonImageWithImage:image];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-05
      • 2017-05-12
      • 2015-01-21
      • 2014-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多