Home > Net >  UIAlertController as child view controller not responding to tap
UIAlertController as child view controller not responding to tap

Time:11-05

So I have a UIAlertController which needs to be presented on top of another view controller, so a child view controller. I can get the child view controller to display on top of the parent no problem, but I can't get the alert buttons to respond when I bring this child view controller to the front.

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;

CodePudding user response:

UIAlertController is a specialized controller that makes use of UIAlertAction.

Worth noting from Apple's enter image description here

CodePudding user response:

below code works for me:


- (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");
}
  • Related