简体   繁体   中英

Objective C Accessing var in Initial View Controller of Storyboard

Using:

UIViewController *vc = [[UIStoryboard storyboardWithName:@"MyStoryboard" bundle:nil] instantiateInitialViewController];

[self presentViewController:vc animated:YES completion:^{}];

But I need to set a BOOL in the initial viewController for that story board.

I have BOOL declared in the .h and an if statement is dependent on it:

if (_boolIsTrue) {
    FirstView *firstView = (FirstView *)[storyboard instantiateViewControllerWithIdentifier:@"first_view"];
    [self.navigationController setViewControllers:@[firstView] animated:NO];
}
else {
    SecondView *secondView = (SecondView *)[storyboard instantiateViewControllerWithIdentifier:@"second_view"];
    [self.navigationController setViewControllers:@[secondView] animated:NO];
}

When I try to access it with something like:

UIStoryboard *storybord = [UIStoryboard storyboardWithName:@"MyStoryboard" bundle:nil];
UIViewController *vc =[storybord instantiateInitialViewController];

vc does not give me access to the BOOL.

Not sure what it is that I am not understanding about ViewControllers and Storyboards, but I would really appreciate the help.

Thanks

EDIT AFTER SOLUTION: Wain was exactly right!

Here's the exact working code since my gaps spanned a couple different concepts. In case it helps someone else:

UINavigationController *nav = [[UIStoryboard storyboardWithName:@“MyStoryBoard” bundle:nil] instantiateInitialViewController];
InitialLockSetupViewController *vc = nav.viewControllers.lastObject;
vc.isBoolTrue = YES;
[self presentViewController:nav animated:YES completion:^{}];

So you have to get the View Controller with the correct type in order to access the var. BUT if it's not set as the initial view controller in the storyboard you have to get it as the root (0-th) view controller on the stack. THEN you have to remember to push the nav controller NOT the view controller.

Thanks again Wain. This was a good lesson!

You're simply not understanding that the compiler needs to know the class type. Currently you're telling the compiler that the class is UIViewController and it obviously doesn't have your custom property. You need to set the class to your custom one, like:

MyViewController *vc = [storybord instantiateInitialViewController];

Also, make sure that you don't call instantiateInitialViewController more than once or you'll have multiple copies of the same class and might use the wrong one...

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