【问题标题】:Targetting a UIButton in a UIView, with addTarget使用 addTarget 在 UIView 中定位 UIButton
【发布时间】:2012-05-19 12:04:29
【问题描述】:

My ViewController.m 创建一个 myUIView 的实例。 在 myUIView 中创建了一个 UIButton。

一切似乎都很好,除了在 ViewController 级别捕获我使用 addTarget 的按钮按下。 按下按钮会导致崩溃,提示:“unrecognized selector sent to instance 0x6c254e0...”

这个 addTarget 代码错了吗?非常感谢任何人的帮助。

- (void)viewDidLoad{

<UIVIew implementation etc...>

[myUIView.myButton addTarget:self action:@selector(myButtonIsPressed:) forControlEvents:UIControlEventTouchUpInside];

}


- (void)myButtonIsPressed{
    NSLog(@"Pressed!");
}

谢谢。

【问题讨论】:

    标签: iphone ios ios5 uikit


    【解决方案1】:

    只需删除:在 myButtonIsPressed 之后:

    [myUIView.myButton addTarget:self action:@selector(myButtonIsPressed:) forControlEvents:UIControlEventTouchUpInside];
    

    【讨论】:

    • 谢谢莎拉! :) 我希望我在 3 小时前问过这个问题。今天早上我本可以做一些有用的事情!
    • 我知道 :) 这是一个愚蠢的错误,我犯了很多次,也浪费了我的时间..!
    • (等待它让我。)谢谢
    • 更多信息,适用于自定义和遇到此帖子的任何其他人: : 不正确的原因是因为它表示您要将变量传递给 myButtonIsPressed 方法。由于 myButtonIsPressed 不接受任何参数,因此您被抛出了一个错误; myButtonIsPressed 和 myButtonIsPressed:(dataType)var 是两种完全不同的方法。希望有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-11
    • 1970-01-01
    • 2020-09-02
    • 2018-06-02
    • 1970-01-01
    相关资源
    最近更新 更多