views:

662

answers:

1

Hi Guys,

I am working on an iPhone application. Application has a navigation bar at the top. I have two views say "View1" and "View2". When you push "View2" view controller the navigation bar shows a back button with the title of the "View1" so you can go back to view1.

Now my question is which event is fired when you click that button? Also is it possible to change the text of that back button in navigation bar?

Any help will be greatly appreciated?

Thanks

A: 

There's no specific event you can get just for that back button. You can use various viewWillAppear:, viewWillDisappear:, etc. methods instead. eg:

//In View2:
- (void) viewWillDisappear:(BOOL) animated {
    [super viewWillDisappear:animated];
    if (!goingToSubscreen) {
        //Do something important here
    }
    goingToSubscreen = NO;
}

//In View1:
- (void) viewWillAppear:(BOOL) animated {
    [super viewWillAppear:animated];
    if (wentToSubscreen) {
        //Do something important here
    }
    wentToSubscreen = NO;
}

Note that the booleans wentToSubscreen and goingToSubscreen should be set up as named. If you are pushing another view onto the navigation controller on top of View2, you should set goingToSubscreen to YES so that it doesn't do the important stuff. Conversely, you should set wentToSubscreen to YES on View1 after pushing View2, so that when it appears again, it's after coming back from that screen that something interesting happens, and not when View1 initially appears.

As for changing the title of the back button, the property belongs to the previous view's back button item:

View1.navigationItem.backBarButtonItem.title = @"My New Title";

or:

View1.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"New Title" style:UIBarButtonItemStyleBordered target:nil action:nil] autorelease];

Your third option is to use a custom button to replace the back button. It will let you specify your own method to call so you don't need to use -viewWillDisappear:, but doing this will show the button as a rectangle instead of a back arrow button. This button goes on View2 instead of View1:

View2.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"New Title" style:UIBarButtonItemStyleDone target:self action:@selector(dismissView)] autorelease];
Ed Marty