ios – Why would not the pushed ViewController take impact when pushing a VC instantly after presenting a VC?

0
1
ios – Why would not the pushed ViewController take impact when pushing a VC instantly after presenting a VC?


I have been writing some demos to check the runtime logic, and I unintentionally found this downside. I could not discover a solution on main boards, nor did I get a consequence from asking AI, so I am right here to hunt assist.

// The binding logic of the button
(void)jumpToSecondViewController { 
    NSLog(@"content material of the navigation stack (earlier than):%@",self.navigationController.viewControllers);

    SecondViewController *secondVC = [[SecondViewController alloc] init];
    secondVC.receivedMessage = @"Howdy World";

    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert" message:@"This is an alert." preferredStyle:UIAlertControllerStyleActionSheet]; 

    UIAlertAction* defaultAction1 = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction action) {
        //[self.navigationController pushViewController:secondVC animated:YES]; 
        NSLog(@"okay"); 
    }]; 

    UIAlertAction defaultAction2 = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
        NSLog(@"no"); 
    }];

    [alert addAction:defaultAction1]; 
    [alert addAction:defaultAction2];

    [self presentViewController:alert animated:NO completion:nil];
    [self.navigationController pushViewController:secondVC animated:NO];

    NSLog(@"content material of the navigation stack (after):%@", self.navigationController.viewControllers);
    
    NSLog(@"right here"); 
}

The logic for the push navigation was initially sure to the UIAlertAction, however I do not perceive why, when positioned in my present place, this system runs usually (it could print “right here”), but the push operation of the navigationController would not work.

But when I push the ViewController earlier than presenting the ViewController, it should work:

// The binding logic of the button
(void)jumpToSecondViewController {
    NSLog(@"content material of the navigation stack (earlier than):%@", self.navigationController.viewControllers);

    SecondViewController *secondVC = [[SecondViewController alloc] init];
    secondVC.receivedMessage = @"Howdy World"; 
    [self.navigationController pushViewController:secondVC animated:NO];

    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert" message:@"This is an alert." preferredStyle:UIAlertControllerStyleActionSheet];

    UIAlertAction* defaultAction1 = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction action) { 
        //[self.navigationController pushViewController:secondVC animated:YES]; 
        NSLog(@"okay"); 
    }];

    UIAlertAction defaultAction2 = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
        NSLog(@"no"); 
    }];

    [alert addAction:defaultAction1];
    [alert addAction:defaultAction2];

    [self presentViewController:alert animated:NO completion:nil]; 
    //[self.navigationController pushViewController:secondVC animated:NO];
    NSLog(@"content material of the navigation stack (after):%@",self.navigationController.viewControllers);
    NSLog(@"right here"); 
}

After pushing first, the introduced alert will seem usually.

// The binding logic of the button
(void)jumpToSecondViewController {
    NSLog(@"content material of the navigation stack (earlier than):%@",self.navigationController.viewControllers);
    /// ......
    NSLog(@"content material of the navigation stack (after):%@",self.navigationController.viewControllers);
    NSLog(@"right here"); 
}

I printed the stack info within the navigationController earlier than and after the operation, and located that when presenting first after which pushing, the pushed content material will not be efficiently added to the stack. I am not fairly positive which space of information I have to find out about, or what inner logic is inflicting this.

LEAVE A REPLY

Please enter your comment!
Please enter your name here