1

In my app I am using two storyboard. Main and Main2.

Main storyboard has two VCs and first one is embedded to a navigation controller. Likewise for Main2 storyboard. -buttonPressed in FirstVC will push to SecondVC in Main. -buttonPressed in SecondVC will present FirstVC of Main2 storyboard. -buttonPressed in FirstVC will push to SecondVC in Main2.

I have a class NetworkConnection which is a shared instance. When reachability changes I'm calling the following method

- (void)internetConnectionAlert {
    NetworkStatus status = [[Reachability reachabilityForInternetConnection] currentReachabilityStatus];

    id obj = [[[UIApplication sharedApplication] keyWindow] rootViewController];

    if ([obj isKindOfClass:[UINavigationController class]] && (status == NotReachable)) {

        UIViewController *viewController = [[obj viewControllers] lastObject];
        [viewController presentViewController:self.alertController
                                     animated:YES
                                   completion:nil];
    } else {
        [self.alertController dismissViewControllerAnimated:YES completion:nil];
    }
}

I run the app. FirstVC of Main comes up. I disconnect the network. AlertController comes over due to reachability changed. I connect back to network. AlertController gets dismissed. Same thing when I go to SecondVC (still in Main). So all is good.

I press button in SecondVC and it goes to FirstVC of Main2. Now when I disconnect the network no AlertController pops up.

So I put some logs around. Turns out when I go from one storyboard to another the rootViewController is not changed to the new navigation controller. The rootViewController is still the navigation controller in Main not in Main2.

This is the log I put in -viewDidLoad of FirstVC in Main2

NSLog(@"Key window rvc : %@",[[[UIApplication sharedApplication] keyWindow] rootViewController]);
NSLog(@"self navi   vc : %@",self.navigationController);

This prints

2016-06-24 18:55:16.622 Navi[12129:3257811] Key window rvc : <UINavigationController: 0x7feaac834800>
2016-06-24 18:55:16.622 Navi[12129:3257811] self navi   vc : <UINavigationController: 0x7feaac832800>

And hence I am getting the following warning

Warning: Attempt to present <UIAlertController: 0x7feaab524490> on <SecondViewController: 0x7feaab657330> whose view is not in the window hierarchy

I do get proper result when I put the following code in -viewDidLoad of FirstVC in Main2.

[[UIApplication sharedApplication] keyWindow].rootViewController = self.navigationController;

I don't think this is any good because I almost definitely will require the original rootViewController at some point in my code. Also when the project gets bigger using the above code in every VC will be a nuisance.

So how do I get the AlertController to show up on the current ViewController (on screen) regardless of the hierarchical mess.

Thank you for your patience.

EDIT : To get a quick overview this is what is happening (don't have the nil problem though).

Code for changing storyboard

- (IBAction)buttonTapped:(id)sender {
    MainTwoViewController *mainTwoVC = [[UIStoryboard storyboardWithName:@"Main2" bundle:nil] instantiateInitialViewController];
    [self presentViewController:mainTwoVC animated:YES completion:nil];
}
Community
  • 1
  • 1
Rishab
  • 1,901
  • 2
  • 18
  • 34

0 Answers0