user2997441
user2997441

Reputation: 195

Popping to view controller, how do I call a method in the view controller I am popping to?

I have a navigation based app and I am trying to pop back to to a certain view controller and it works fine, however I am trying to call a method (account) in the view controller I am popping to. The method gets called however when I push to the view controller ,but doesn't get call when I pop to it. What can I do to call the method when I pop to the view controller?

CurrentViewController.m

 -(void)viewControllerPop{
    ViewController *viewController = [[ViewController alloc]init];
    [viewController account];
    [self.navigationController popViewControllerAnimated:YES];
    }

Upvotes: 1

Views: 1597

Answers (1)

danh
danh

Reputation: 62686

Three answers:

1 Consider not doing it. The view controller you're popping to should observe a change in the model (affected by the pushed vc) and change it's views accordingly in viewWillAppear.

2 Make the the view controller you're popping to a delegate of the pushed vc, define a protocol message that tells the delegate about the condition so it can change it's views.

3 The worst answer, IMO, but you can inspect the navigation controller's view controller stack and get a pointer to the vc that pushed you. (The code you posted is mixed up, it won't do any good to allocate a new vc, change one of it's views and then pop to the one that pushed you. That new viewController you're allocating lives for only an instant, and is immediately discarded).

-(void)viewControllerPop {
    NSArray *viewControllers = [self.navigationController viewControllers];
    NSUInteger count = viewControllers.count;
    ViewController *vcUnderMe = viewControllers[count-1];  // this top one is at count-1

    // I disagree with this in principal, but here's what you're asking
    [vcUnderMe account];         // not sure what this method does... not named well
    vcUnderMe.accountLabel.hidden=NO;

    [self.navigationController popViewControllerAnimated:YES];
}

Upvotes: 1

Related Questions