0

我创建了一个名为 toolbarViewController 的 UIViewController 子类,我在这个视图控制器中声明了一个 UIButton 并指定它的目标如下

UIButton *button = [[UIButton alloc]initWithFrame:myFrame];
[button addTarget:self action:@selector(doSomething) forContorlEvents:UIControlEventTouchUpInside];

然后我在不同的视图控制器中执行以下操作

toolbarViewController *toolbar = [[toolbarViewController alloc]init];
[self.view addSubview:toolbar.view];

问题是当我按下按钮时出现异常:无法识别的选择器(doSomething)发送到实例,我在这里做错了什么?

toolbarViewController.h 中的 doSomething 声明

-(void)doSomething;

在toolbarViewController.m

-(void)doSomething{ NSLog("doSomething got called"); }
4

3 回答 3

1

检查doSomething. 如果您声明 IBAction 之类-(IBAction)doSomething:(id)sender的,则应添加选择器,例如

[button addTarget:self action:@selector(doSomething:) forContorlEvents:UIControlEventTouchUpInside];

代替

[button addTarget:self action:@selector(doSomething) forContorlEvents:UIControlEventTouchUpInside];
于 2012-06-04T06:40:35.647 回答
1

如果你使用ARC,你分配toolbarViewController,但它有relese。你可以试试这个:

ViewController.h
@interface ViewController : UIViewController{
    toolbarViewController * toolbar;
}
@property(nonatomic,strong) toolbarViewController * toolbar;


ViewController.m
@synthesize toolbar = _toolbar;


    toolbar = [[toolbarViewController alloc]initWithNibName:@"toolbarViewController" bundle:[NSBundle mainBundle]];

你分配按钮可以做到这一点:

UIButton *bt = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        bt.frame = myFrame;
        [bt addTarget:self action:@selector(dosomething) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:bt];
于 2012-06-04T07:00:54.397 回答
0

一种可能的情况是,如果您将 a 添加UITapGestureRecognizerUIButton的超级视图。在 iOS5 上,似乎存在一个问题,UITapGestureRecognizers即不取消视图中的点击。

所以你需要做的就是设置

tapGesture.cancelsTouchesInView = NO;

让我知道它是否有帮助!

于 2013-05-15T01:11:55.067 回答