Detect when Viewcontroller was clicked

I am trying to detect when the ViewController was clicked. So I followed the Apple document http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationBarDelegate_Protocol/Reference/Reference.html , about the NavegationBar delegate, but I did not understand how to make it successful. I put the following code on my code in my ViewController, but did not find it pushing. What am I doing wrong?

- (void)navigationBar:(UINavigationBar *)navigationBar didPushItem:(UINavigationItem *)item, { NSLog(@"didPushItem: %@", item); [self showimage]; } 
+4
source share
3 answers

It is not clear what you need to do, but there are several UIViewController methods to determine its context. There are two below and a couple more in 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); } 
+11
source

You must implement the UINavigationControllerDelegate for the 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 specific method you need, which would be like " navigationController:didPushViewController:animated: ", does not exist in the protocol.

However, I believe that you can achieve the desired behavior with navigationController:willShowViewController:animated: Note that this method is called before the view is displayed for the UIViewController and after it is UIViewController the UINavigationController stack.

+5
source

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

If you want more control over 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> } } 

They will be called immediately before and after the navigation controller pushes / pushes the child view controller.

From the documents ...

didMoveToParentViewController:

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

- (void)didMoveToParentViewController:(UIViewController *)parent

Options

the parent

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

Discussion

The controller of your view can override this method when it wants to respond to being added to the container.

and...

willMoveToParentViewController:

Called immediately before adding or removing a view controller from the container controller.

- (void)willMoveToParentViewController:(UIViewController *)parent

Options

the parent

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

Discussion

The controller of your view can override this method when it needs to know that it is added to the container.

+3
source

All Articles