UIButton没有调用目标方法

时间:2012-07-27 10:29:15

标签: objective-c uibutton

我有以下代码:

UIImage *registerImg = [UIImage imageNamed:@"register_button_normal"];
CGFloat registerOffsetX = (view.frame.size.width - registerImg.size.width) / 2;

UIButton *registerBtnTwo = [[UIButton alloc] init];
registerBtnTwo.frame = CGRectMake(registerOffsetX, 111, registerImg.size.width, registerImg.size.height);

[registerBtnTwo setBackgroundImage:registerImg forState:UIControlStateNormal];
[registerBtnTwo addTarget:self action:@selector(submitRegister) forControlEvents:UIControlEventTouchUpInside];
[registerPanel addSubview:registerBtnTwo];
[registerBtnTwo release];

我在类标题中有- (void)submitRegister;作为实例方法;

问题是事件没有被触发,因为我的submitRegister实现中有NSLog

我尝试了什么:

1. UIButton *registerBtnTwo = [UIButton buttonWithType:UIButtonTypeCustom];

2. UIButton *registerBtnTwo = [[UIButton alloc] initWithFrame:frame];

编辑1:我将当前的View Controller添加到我的navigationController?

if ( row == kLoginRegisterIndex ) {
    login = [[LoginRegisterVC alloc] init];
    [self.navigationController pushViewController:login animated:YES];
}  

编辑2:

UIView *registerPanel = [[UIView alloc] initWithFrame:frame];
registerPanel.backgroundColor = [UIColor colorWithPatternImage:image];

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

尝试更改

[registerBtnTwo addTarget:self action:@selector(submitRegister) forControlEvents:UIControlEventTouchUpInside];

[registerBtnTwo addTarget:self action:@selector(submitRegister:) forControlEvents:UIControlEventTouchUpInside];

方法签名为submitRegister:(不是submitRegister)。 您无需在标题中声明方法。

如果这无助于检查主机视图的帧。当按钮位于框架之外时,它可以显示但不接受触摸。

我不明白你在编辑中提出的要求。

答案 1 :(得分:0)

问题是父视图是UIImageView,默认情况下有userInteractionEnabled = NO;