2012-07-27 61 views
1

我有以下代碼:的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:我是誰當前視圖控制器添加到我的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]; 

任何想法?

+0

請給我看你分配和初始化registerPanel的代碼。這是一個UIView? – dasdom 2012-07-27 11:21:32

回答

0

問題是,父視圖是UIImageView默認情況下有userInteractionEnabled = NO;

1

嘗試改變

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

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

方法簽名爲submitRegister:(未submitRegister)。 您不需要在標題中聲明該方法。

如果這無助於檢查主機視圖的框架。當按鈕在框架外部時,它可以被顯示,但不會接受觸摸。

我不明白你在編輯中要求什麼。

+0

1.'[registerBtnTwo addTarget:self action:@selector(submitRegister :) forControlEvents:UIControlEventTouchUpInside];'如果你想將當前按鈕傳遞給'submitRegister'方法 - 如果我有多個按鈕調用一個方法會很有用;儘管第一點我已經嘗試過了,但它不起作用。我會檢查框架。 – h4cky 2012-07-27 11:18:20

+0

你試過了嗎? – dasdom 2012-07-27 11:19:17

+0

我試圖改變它:) – h4cky 2012-07-27 11:19:54