Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get notification when a view controller pop or push in to the navigation controller stack

I want to get notification when a view controller pop or push in the navigation controller stack. I tried to use

- (void)setViewControllers:(NSArray *)viewControllers

But I failed. And I do not want to use delegate method to achieve this...

like image 395
Draven Zuo Avatar asked Jun 30 '14 15:06

Draven Zuo


People also ask

How do I embed a view controller in navigation controller storyboard?

Storyboard setup In your storyboard, select the initial view controller in your hierarchy. With this view controller selected, choose the menu item Editor -> Embed In -> Navigation Controller .

How do I add a view controller to my navigation controller?

The Navigation Controller will automatically contain a Table View Controller, delete this and drag a View Controller to the storyboard. Ctrl + drag from the Navigation Controller to the View Controller and select the “root view controller” segue. Select the View Controller's view and Go to the Attributes inspector.

How do I push a view controller?

Pushing a view controller causes its view to be embedded in the navigation interface. If the animated parameter is true , the view is animated into position; otherwise, the view is simply displayed in its final location.

How do I know if my view controller is root view controller?

Accessing the Root View Controller But it's easy to do. The root view controller is simply the view controller that sits at the bottom of the navigation stack. You can access the navigation controller's array of view controllers through its viewControllers property.


1 Answers

You can subclass UINavigationController and override some methods:

- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"poped" object:nil userInfo:@{}];
    return [super popToRootViewControllerAnimated:animated];
}

- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"poped" object:nil userInfo:@{}];
    return [super popToViewController:viewController animated:animated];
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"poped" object:nil userInfo:@{}];
    return [super popViewControllerAnimated:animated];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushed" object:nil userInfo:@{}];
    return [super pushViewController:viewController animated:animated];
}

i left user info like @{}, but you can place there something if You want, like controller that was added or removed.

I don't know, but I think You should think twice, if You architecture need notifications for such situation.

Also You have to check does pop methods call each other, in such situation you can get few notifications for one pop.

like image 71
in.disee Avatar answered Oct 09 '22 22:10

in.disee