iPhone UIButton操作无效

时间:2012-12-22 16:20:32

标签: objective-c uiview uibutton

我创建了UIView,然后我将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>
)}

1 个答案:

答案 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.");
}