views:

800

answers:

3

The default behaviour when pushing a UIViewController on a UINavigationController is for the OS to display a back button that pops the UIViewController off again.

I have the desire to set a different behavior for this back button (to go back two screens) - is there anyway I can do this without having to create my own back button with custom graphic etc.

Thanks :)

A: 

As I half suspected originally, this isn't possible any exceptionally easy way. So same method applies when creating any custom UIBarButtonItem, just have to source the back button icon from Google....

UIButton *backButtonInternal = [[UIButton alloc] initWithFrame:CGRectMake(0,0,54,30)];
[backButtonInternal setBackgroundImage:[UIImage imageNamed:@"backButton.png"] forState:UIControlStateNormal];
boldSystemFontOfSize:12]];
[backButtonInternal addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:backButtonInternal];   
[backButtonInternal release];
[[self navigationItem] setLeftBarButtonItem:backBarButton];
[backBarButton release];
adam
A: 

Anything wrong with UIViewController's navigationItem property? Here's how I get a cancel button, for example:

self.navigationItem.leftBarButtonItem =
  [[[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemCancel
                                               target: self
                                               action: @selector(cancel)] autorelease];  
jamie
A: 

Using the "leftBarButtonItem" allows you to set the target and selector. But if you set the "backBarButtonItem" on the previous controller, the target and selector will be ignored. However, the leftBarButtonItem does not have the left pointing arrow.

gstroup