所以我有一个
UIAlertController
需要呈现在另一个视图控制器之上,所以是一个子视图控制器。我可以让子视图控制器显示在父视图之上,没有问题,但是当我将此子视图控制器放在前面时,我无法让警报按钮做出响应。
UIAlertController* alert = [AlertHelper createAlertWithTitle:title
message:message
cancelButton:nil
continueButtonText:Ok
continueAction:nil
cancelAction:nil];
alert.view.translatesAutoresizingMaskIntoConstraints = false;
[self addChildViewController:alert];
alert.view.frame = self.view.bounds;
[self.view addSubview:alert.view];
[alert didMoveToParentViewController:self];
[alert.view.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor].active = YES;
[alert.view.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor].active = YES;
下面的代码对我有用:
- (void)viewDidLoad {
[super viewDidLoad];
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:@"Logout"
message:@"Are You Sure Want to Logout!"
preferredStyle:UIAlertControllerStyleAlert];
alert.view.translatesAutoresizingMaskIntoConstraints = false;
alert.view.frame = self.view.bounds;
[self.view addSubview:alert.view];
[self addChildViewController:alert];
UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:@"Yes"
style:UIAlertActionStyleDefault
handler: nil];
[alert addAction:yesButton];
[alert didMoveToParentViewController:self];
[alert.view.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor].active = YES;
[alert.view.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor].active = YES;
UIView *yesButtonView = (UIView*)[yesButton valueForKey:@"__representer"];
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleSingleTap:)];
[yesButtonView addGestureRecognizer:singleFingerTap];
}
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
{
NSLog(@"yesssss");
}