iOS - UIButton作为UIAlertController上的子视图不触发事件

时间:2017-11-22 07:58:45

标签: ios objective-c uibutton uialertcontroller

我在UIAlertController上添加UIButton,但它没有在任何事件上触发该方法。

UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[cancelBtn setFrame:CGRectMake((_alertController.view.frame.size.width/2)-30, _alertController.view.frame.size.height-50, 60, 30)];
[cancelBtn setTitle:@"Cancel" forState:UIControlStateNormal];
[cancelBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[cancelBtn setBackgroundColor:[UIColor orangeColor]];
[cancelBtn addTarget:self action:@selector(forceFullDismiss_FireDelegate:) forControlEvents:UIControlEventTouchUpInside];
cancelBtn.showsTouchWhenHighlighted = YES;

[_alertController.view setUserInteractionEnabled:YES];
[_alertController.view addSubview:cancelBtn];

按钮似乎突出显示但相应的事件未触发。

这里有什么问题?

1 个答案:

答案 0 :(得分:1)

这是UIAlertController documentation

  

UIAlertController类旨在按原样使用,但不是   支持子类化。此类的视图层次结构是私有的   不得修改。

如果您想添加取消按钮,可以使用

UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    //  action on cancel
}];

[_alertController addAction:cancel];