0

我创建 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>
)}
4

1 回答 1

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.");
}
于 2012-12-22T16:54:45.560 回答