Inbl
Inbl

Reputation: 660

Trying to set property in parent viewcontroller, but it ends up being Null

I'm in a view controller that was pushed onto the stack by by a navigation controller.

I have this code that runs when the back button is pressed:

- (void)didMoveToParentViewController:(UIViewController *)parent {
    if (![parent isEqual:self.parentViewController]) {            
        //NSLog(@"Back pressed");
        MyCustomViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"TableVC"];
        vc.index = [_itemChooser selectedRowInComponent:0];
    }    
}

I'm trying to set index to the same index of what I chose in the picker view. If I NSLog the value here, it is correct, but if I NSLog the value in viewWillAppear in the parent, it is 0. (It prints as Null if I'm using an object rather than an NSUInteger)

Pushed it on like this:

[self.navigationController pushViewController:pickervc animated:YES];

Upvotes: 0

Views: 401

Answers (1)

Inbl
Inbl

Reputation: 660

I solved my problem by defining a view controller property in the child class, and assigning self to it before pushing. I was then able to set/access properties from the parent in the child no problem.

Upvotes: 1

Related Questions