Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect when Viewcontroller was Pushed

I am trying to detect when a ViewController was pushed. So I followed the doc of Apple http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationBarDelegate_Protocol/Reference/Reference.html , about NavegationBar delegate but I didn´t figured out how to make it working successfully. I placed on my code the following code in my ViewController but it doesn't detect it was pushing. What I am doing wrong ?

- (void)navigationBar:(UINavigationBar *)navigationBar didPushItem:(UINavigationItem *)item, {
    NSLog(@"didPushItem: %@", item);
    [self showimage];
}
like image 254
Ben Avatar asked Aug 12 '12 13:08

Ben


3 Answers

Not clear what you are needing to do but there are several UIViewController methods for discerning its context. There are two below and a couple more in the docs

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    BOOL pushed = [self isMovingToParentViewController];

    printf("viewWillAppear     %d\n", pushed);

}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    BOOL popped = [self isMovingFromParentViewController];

    printf("viewWillDisappear     %d\n", popped);

}
like image 86
spring Avatar answered Oct 28 '22 11:10

spring


You should implement UINavigationControllerDelegate for UIViewController and UINavigationController related tasks.

Here is the link to the documentation: http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UINavigationControllerDelegate_Protocol/Reference/Reference.html

The particular method you want, which would be something like "navigationController:didPushViewController:animated:", does not exist in the protocol.

However, I believe you can achieve the desired behavior using the navigationController:willShowViewController:animated:. Note that this method gets called before the View for the UIViewController is shown and after it has been pushed into the UINavigationController stack.

like image 35
Cezar Avatar answered Oct 28 '22 13:10

Cezar


The -viewWillApear method is reasonable, but it gets called when the view is about to be inserted into the view hierarchy, which may or may not be what you want.

If you want more control of the push/pull progress, you can override

- (void)willMoveToParentViewController:(UIViewController *)parent {
    if (nil == parent) {
        // Moving to nil parent means being removed from parent
    } else {
        // Will be inserted as a child view controller of <parent>
    }
}

- (void)didMoveToParentViewController:(UIViewController *)parent {
    if (nil == parent) {
        // Moving to nil parent means was just removed from parent
    } else {
        // Was just inserted as a child view controller of <parent>
    }
}

These will be called just before and after the navigation controller pushes/pops the child view controller.

From the docs...

didMoveToParentViewController:

Called after the view controller is added or removed from a container view controller.

- (void)didMoveToParentViewController:(UIViewController *)parent

Parameters

parent

The parent view controller, or nil if there is no parent.

Discussion

Your view controller can override this method when it wants to react to being added to a container.

and...

willMoveToParentViewController:

Called just before the view controller is added or removed from a container view controller.

- (void)willMoveToParentViewController:(UIViewController *)parent

Parameters

parent

The parent view controller, or nil if there is no parent.

Discussion

Your view controller can override this method when it needs to know that it has been added to a container.

like image 3
Jody Hagins Avatar answered Oct 28 '22 11:10

Jody Hagins