Yevgeni
Yevgeni

Reputation: 1533

NavigationController changing back button

I have two Questions :

Question 1: I'm trying to change the Navigation back button, i am doing it from my parent ViewController:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"imgrdy"]) {
        VCImageEditingViewController *ieVC = (VCImageEditingViewController *)segue.destinationViewController;
        UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: @"New" style:UIBarButtonItemStyleBordered target:nil action:@selector(handleNewBtnNav:)];
        [[self navigationItem] setBackBarButtonItem: newBackButton];    
    }
}
-(IBAction)handleNewBtnNav:(id)sender {
    UIAlertView *alertNew = [[UIAlertView alloc]initWithTitle:@"New photo" message:@"Are you sure?" delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];
    [alertNew show];
}

The title has indeed changed but the alert isn't shown. I've tried to change the target to "self" and "ieVC" and still nothing.

Question 2: Is there a way to change the title of the back button only of one ViewController? or do i have to do the same change on each parent ViewController?

Thank you.

Upvotes: 0

Views: 143

Answers (1)

Antonio MG
Antonio MG

Reputation: 20410

Use target:self when declaring the button, that is the receiver of the action and you are saying that is nil

UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: @"New" style:UIBarButtonItemStyleBordered target:self action:@selector(handleNewBtnNav:)];

Upvotes: 1

Related Questions