2012-12-22 34 views
0

我創建UIView,然後添加UIButton作爲操作的子視圖,但此操作永遠不會被調用。iPhone UIButton操作不起作用

有人知道爲什麼嗎?

這裏是我的代碼:

- (void)viewDidLoad{ 

    UIView *roundResultView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 200, 150)]; 
    UIImageView *_popUpBg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"NotificationBg.png"]]; 
    _popUpBg.frame = CGRectMake(0, 0, roundViewWidth, roundViewHeight); 

    UIButton *closeButton = [[UIButton alloc]initWithFrame:CGRectMake(30, 30, 100, 30)]; 
    [closeButton addTarget:self action:@selector(closeAndBack) forControlEvents:UIControlEventTouchUpInside]; 
    [closeButton setTitle:@"Back") forState:UIControlStateNormal]; 
    [roundResultView addSubview:_popUpBg]; 
    [roundResultView addSubview:closeButton]; 
    [self.view addSubview:roundResultView]; 
} 


-(void)closeAndBack{ 
    NSLog(@"closeAndBack"); //never called 
} 

我已經檢查了這一點:

NSLog(@"%@", [closeButton allControlEvents]); 
NSLog(@"%@", [closeButton allTargets]); 

和它打印:

... Sedmica[2192:14f03] 64 
... Sedmica[2192:14f03] {(
    <ViewController: 0x8631e10> 
)} 
+0

你嘗試過使用viewDidLoad和closeAndBack中的斷點嗎? –

+0

不,我不知道如何使用斷點。你能給我建議如何做到這一點? – CroiOS

+0

只在外側/牆上點擊左側,將創建藍色斷點。然後你可以跨過。在底部會有很多按鈕跨越,步入等...... –

回答

0

更新你的代碼這一點,請讓我知道:

UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[closeButton addTarget:self 
       action:@selector(closeAndBack:) 
     forControlEvents:UIControlEventTouchDown]; 
[closeButton setTitle:@"Back" 
      forState:UIControlStateNormal]; 
closeButton.frame = CGRectMake(30.0, 30.0, 100.0, 30.0); 
[self.view addSubview:closeButton]; 

- (IBAction)closeAndBack:(UIButton *)button{ 
     NSLog(@"Button clicked."); 
} 
+0

沒有點擊效果與我的代碼一樣。就像有些東西在那個按鈕上,我不能點擊它。 – CroiOS

+0

刪除_popUpBg子視圖並嘗試 –

+0

沒有效果。我甚至嘗試:[self.view bringSubviewToFront:roundResultView]; – CroiOS