larrysanchez
larrysanchez

Reputation: 127

Pass variable from parent view to child view through uinavigationcontroller

Sorry if this seems like a simple question, but every time I search on google for this subject all I can find is how to pass data from a child view to a parent view, not vice versa.

Basically, I am presenting a modal view, which is in its own UINavigation controller. The modal view needs to know if it is allowed to be 'edited' by the user or not - so I thought the most simple way to do this would be to set a BOOL on the child view (isEdit) to TRUE (this would get set depending on the segue that is occurring) However because this modal view is being presented from a UINavigation controller - I cannot access it from the -(void)prepareForSegue function and set the BOOL directly. I have tried subclassing the UINav controller, and including a BOOL in it that can be set, then checked back in the child view, but I cannot seem to access the controller from the child view - I would include my code, but I am sure there must be a more simple way of accomplishing this task!

Is anyone able to suggest a more economical way of passing this boolean 'through' the UINavigationController to the child view?

Thanks

Upvotes: 0

Views: 2246

Answers (2)

followben
followben

Reputation: 9197

If your segue invokes a navigation controller, you can set properties on the root view controller from -prepareForSegue, like so

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"MySegueId"]) {
        UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
        MyViewController *childController = (MyViewController *)navController.childViewControllers.lastObject;
        childController.isEditable = YES;
    }
}

Upvotes: 5

Firo
Firo

Reputation: 15566

When you alloc/init the modal view controller just set the property then (say your modal view controller is called ProfileVC):

ProfileVC *vc = [[ProfileVC alloc] init];
vc.isEdit = YES;
[vc.navigationController presentModalViewController:vc animated:YES];

just make sure in your ProfileVC.h you have a property called isEdit:

@interface ProfileVC : UIViewController
@property (nonatomic) BOOL isEdit;
@end

Upvotes: 0

Related Questions