1

I am trying to change the action executed by pressing the backBarButtonItem of a Navigation Controller. I know that i have to edit the backBarButtonItem before the next view (on which the button with custom behavior should appear) is pushed. So in the previous ViewController i added the following code, to push via segue:

#pragma mark - segue methods

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"SettingsToProfile"]) {
        MyProfileViewController* myprofileVC = [segue destinationViewController];
        myprofileVC.myProfile = myProfile;
        self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Settings_" style:UIBarButtonItemStylePlain target:myprofileVC action:@selector(popTOSettingsViewController:)];
    } else {
        self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Settings" style:UIBarButtonItemStylePlain target:nil action:nil];
    }
}

The title "Settings_" gets displayed correctly, but "target: myprofileVC action:@selector(popTOSettingsViewController:)" doesn't seem to have any effect at all.

Background:

MyProfileViewController is a View, where the user can edit his/her own information. Whenever the backbarbutton is clicked, i want to check if something in the GUI has been changed by the user. If thats the case, a UIAlterView should ask the user, if he/she wants to save the changing. I tried to work it out with, viewWillDissappear, but the AlterView gets displayed in the next ViewController (and program crashes, if i click on the alterViewButtons).

FimS
  • 13
  • 3

1 Answers1

0

I'm not sure that you can change the target of a backBarButtonItem. How about self.navigationItem.leftBarButtonItem instead? There's a discussion at self.navigationItem.backBarButtonItem not working ?? Why is the previous menu still showing as the button? that may be relevant.

Community
  • 1
  • 1
emrys57
  • 6,679
  • 3
  • 39
  • 49
  • I had it working with a leftBarButtonItem, but i am quite uncomfortable to change the appearance of it to a back button. I am just wondering, why the action (on other target) is not working. – FimS Dec 14 '12 at 16:03
  • The system does not use the target and selector you set for a backBarButtonItem. Someone else had the same problem at http://www.cocoabuilder.com/archive/cocoa/297620-overriding-target-and-action-of-backbarbuttonitem.html – emrys57 Dec 14 '12 at 16:10
  • Thanks for the info, so way to go with leftBarButton :) – FimS Dec 14 '12 at 16:13