Search code examples
iphonexcodeuiviewcontrolleruinavigationcontrollernsnotifications

How to add a notification for pop view controller in iphone application?


I have seen the sample application of iPhone MP-movie player - controller.

They have added a notification on the sample code.

// Register to receive a notification that the movie is now in memory and ready to play
[[NSNotificationCenter defaultCenter] addObserver:self 
                 selector:@selector(moviePreloadDidFinish:) 
                 name:MPMoviePlayerContentPreloadDidFinishNotification 
                 object:nil];

In above code, When MPMoviePlayerController finishes loading, it invokes moviePreloadDidFinish method.

Similarly, I want to fire an method when user press back button from navigation bar, (back to previous view controller through navigation controller ).

I don't know how to add a notification for that.


Solution

  • Put your own custom back button in the navigationItem:

    UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithImage:yourImage style:UIBarButtonItemStyleBordered target:self action:@selector(goBack)];
    self.navigationItem.leftBarButtonItem = btn;
    [btn release];
    

    In the goBack method of your viewController, you'll put whatever code you need and then pop the viewController:

    - (void)goBack {
     /* your code here */
    
    [self.view.navigationController popToRootViewControllerAnimated:YES];
    }