Understanding properties in a UINavigationController with viewWillAppear and viewWillDisappear

88 Views Asked by At

I have a UINavigationController that I reuse to push photos and comments looping over each other through the navigation controller.

In my MyViewController.h:

@property (nonatomic, strong) NSMutableAttributedString *pLabel;

In my MyViewController.m:

- (void) viewWillDisappear:(BOOL)animated
{
    if ([self.navigationController.viewControllers indexOfObject:self] == NSNotFound)
    {
        // A photo function gets the right label
        _pLabel = // function gets correct label I want
    }

    [super viewWillDisappear:animated];
}

- (void)viewWillAppear:(BOOL)animated
{
    _pLabel = // is already set
}

I click on MyViewController once and set mvc.pLabel = @"1st one". Then I click a button to create a new MyViewController and set mvc.pLabel = @"2nd";

Then when I click the Back button viewWillDisappear gets called and my dictionary sets pLabel = @"1st". Then viewWillAppear gets run to show the first navigation controller view and pLabel = @"2nd";

Why isn't viewWillDisappear not saving the pLabel?

Thanks.

1

There are 1 best solutions below

0
On

Your property is bound to 1 instance of MyViewController so when you pop your second view controller you'll set the property of the MyViewController that will disappear and not to the one that'll become visible.

If you don't keep a reference on the MyViewController that disappears there are no reason to update one of its label, since you'll probably show a fresh new instance of MyViewController next time.