1

How can I popview from back button pressed, i passed the below code from previous view controller. Thanks

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
         initWithTitle:@"Back"
         style:UIBarButtonItemStyleBordered
         target:nil action:nil];
Cœur
  • 37,241
  • 25
  • 195
  • 267
Goods
  • 11
  • 4

1 Answers1

3

There's no need to manually add a back button.

But if you really need that custom button to pop the view controller, you could make a custom message.

-(void)popViewControllerWithAnimation {
  [self.navigationController popViewControllerAnimated:YES];
}
...
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
     initWithTitle:@"Back"
     style:UIBarButtonItemStyleBordered
     target:self action:@selector(popViewControllerWithAnimation)];

Or create an NSInvocation.

NSInvocation* invoc = [NSInvocation invocationWithMethodSignature:
      [self.navigationController methodSignatureForSelector:@selector(popViewControllerAnimated:)]];
// watch out for memory management issues: you may need to -retain invoc.
[invoc setTarget:self.navigationController];
[invoc setSelector:@selector(popViewControllerAnimated:)];
BOOL yes = YES;
[invoc setArgument:&yes atIndex:2];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
     initWithTitle:@"Back"
     style:UIBarButtonItemStyleBordered
     target:invoc action:@selector(invoke)];
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • I thought that `target` s and `action` s on a `UIBarButtonItem` that's used as the `backBarButtonItem` were ignored... I haven't tested it, but if that's the case, your code, and any other code trying to override the Back button, won't work. (Only the `title` is used for the `backBarButtonItem` afaik.) – Douwe Maan May 16 '10 at 14:10
  • @DouweM: Ah right. Use the `leftBarButtonItem` then. Anyway I don't see a reason to override the default back button. – kennytm May 16 '10 at 14:12
  • No, me neither, but that's what the OP and your code were trying to do ;) – Douwe Maan May 16 '10 at 14:19