简体   繁体   中英

UINavigationViewController setViewControllers not setting correctly

I am having an issue where calling setViewControllers does not set my array of view controllers to my navigation view controller. I am doing this in a block, so I'm not sure if it's a UI issue (but I have tried solving this by setting my view controllers in the main queue).

[self thisIsABlock:^(){
        UIViewController3 *viewController3 = [storyboard instantiateViewControllerWithIdentifier:@"HeyHey"];
        // viewController3 is not nil

        NSMutableArray *array1 = [self.navigationController.viewControllers mutableCopy];
        // array1 contains [viewController1, viewController2]

        [array1 addObject:viewController3];
        [self.navigationController setViewControllers:array1 animated:NO];
        // Now array1 contains [viewController1, viewController2, viewController3] 

        NSMutableArray *array2 = [self.navigationController.viewControllers mutableCopy];
        // array2 only contains [viewController1, viewController2]
}];

This block of code is an abstraction of what I'm doing in my codebase. I may be missing some crucial code that is causing this issue but I hope that someone has encountered this problem before and knows what the problem is. Thanks!

// EDIT 1: I am calling this at - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

When I open the app from background to foreground, my code works.

Above code is correct, but you can provide valid data like this,

YourViewController *viewController3 = [self.storyboard instantiateViewControllerWithIdentifier:@"YourStoryBoardViewControllerIdentifer"];

hope it helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM