1
-(void)viewDidAppear:(BOOL)animated
{
    UIBarButtonItem *navigationBarbackButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(backToRootView:)];
    self.navigationItem.hidesBackButton = YES;
    self.navigationItem.backBarButtonItem = navigationBarbackButton;
}


-(void)backToRootView:(UIBarButtonItem *)sender
{

    [self.navigationController popToRootViewControllerAnimated:YES];
}
Kampai
  • 22,848
  • 21
  • 95
  • 95
harish
  • 21
  • 3

1 Answers1

0

Here contradiction is that you are setting action event to backBarButtonItem and also set it to hidden. So it is not visible right now.

Remove below line from viewDidAppear() method:

self.navigationItem.hidesBackButton = YES;

Also it is good programming to call super class method for view life cycle methods.

So updated method will be:

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    UIBarButtonItem *navigationBarbackButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(backToRootView:)];
    self.navigationItem.backBarButtonItem = navigationBarbackButton;
}

Edit:

backBarButtonItem has default action to pop controller, you don't need to add an action event for that. I'm not sure about your concern to use back button but if you are trying customise left button item then must follow this code:

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    UIBarButtonItem *navigationBarbackButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(backToRootView:)];
    self.navigationItem.leftBarButtonItem = navigationBarbackButton;
}


-(void)backToRootView:(UIBarButtonItem *)sender
{

    [self.navigationController popToRootViewControllerAnimated:YES];
}
Kampai
  • 22,848
  • 21
  • 95
  • 95
  • backBarButtonItem action can't be change. To use custom action or look must have to use leftBarButtonItem instead of backBatButtonItem – Bhumeshwer katre Dec 11 '14 at 06:50
  • Yes. you are right that's why I haven't include action method. But if OP's concern is with just to change left bar button item then let me update my answer. – Kampai Dec 11 '14 at 06:56