开发者

How do I specify an action for a custom Back button in a standalone UINavigationBar?

I have a custom UINavigationBar on a screen, i.e., no navigation controll开发者_如何转开发er, as defined below which contains a Back button w/ the title "Media" and the action "mediaViewComplete". However, the mediaViewComplete method is not being called when the button is tapped. How do you specify an action for a Back button?

self.navigationBar=[[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)];
navigationBar.barStyle=UIBarStyleBlackTranslucent;

UINavigationItem *navigationItem=[[UINavigationItem alloc]init];
UIBarButtonItem *backButton=[[UIBarButtonItem alloc]initWithTitle:@"Media" style:UIBarButtonItemStylePlain target:self action:@selector(mediaViewComplete)];
navigationItem.backBarButtonItem=backButton;
[self.navigationBar pushNavigationItem:navigationItem animated:NO];

navigationItem=[[UINavigationItem alloc]init];
UIBarButtonItem *editButton=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editCategorization)];
navigationItem.rightBarButtonItem=editButton;
[self.navigationBar pushNavigationItem:navigationItem animated:NO];

[self.view addSubview:navigationBar];

[editButton release];
[backButton release];
[navigationItem release];


The problem is that you are using:

navigationItem.backBarButtonItem = backbutton;

The backBarButtonItem has it's own event that is not overridden by your action: option. In fact, Apple guidelines state that you should use "nil" as you action for backBarButtonItem. The backBarButtonItem is not owned by the current view controller.

If you are unconcerned about the arrow shape of the button, you should use leftBarButtonItem instead. If you want to create fully custom back buttons with the arrow shape, you'll have to do some custom magic. Luckily, most of the work has been done for you:

http://idevrecipes.com/2011/01/12/how-do-iphone-apps-instagramreederdailybooth-implement-custom-navigationbar-with-variable-width-back-buttons/

Download this project and look at the results. It even contains the images you need to retain an arrow shaped custom back button. It is more work, but it's much less hack.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜