简体   繁体   中英

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];
}

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);

}

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM