UINavigationItem BackButtonItem不显示

时间:2011-10-12 22:48:25

标签: ios4 uibutton uinavigationitem

使用以下代码我正在实现自己的导航栏。出于某种原因,当我运行我的应用程序时,没有任何东西出现在导航栏上的后退(左箭头)按钮。但是,如果我将代码更改为leftBarButtonItem,则会出现按钮。

// Draw Navigation Bar
UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[navigationBar setDelegate:self];

UINavigationItem *navigationItem = [[UINavigationItem alloc] init];

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                                style:UIBarButtonItemStylePlain
                                                               target:nil 
                                                               action:nil];
navigationItem.backBarButtonItem = backButton;
[navigationBar pushNavigationItem:navigationItem animated:NO];
[self.view addSubview:navigationBar];
[navigationBar release];
[backButton release];

2 个答案:

答案 0 :(得分:3)

backBarButtonItem由父ViewController设置。 换句话说,它不是由你看到它的ViewController设置的,而是由它所指向的ViewController设置的。因此,如果您的ViewController是排在第一位的,那么它就没有后退按钮。

另外,您自己创建了一个NavigtionBar。这通常不是要走的路,UINavigationBar不是像按钮或标签这样的UI元素。您应该使用UINavigationController来处理ViewControllers的所有推送和弹出。

答案 1 :(得分:1)

想出来:

// Draw Navigation Bar
UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[navigationBar setDelegate:self];

UINavigationItem *navigationItem = [[UINavigationItem alloc] init];

UIButton *button = [UIButton buttonWithType:101];
// add selector
[button setTitle:@"Back" forState:UIControlStateNormal];

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:button];

navigationItem.leftBarButtonItem = backButton;
[navigationBar pushNavigationItem:navigationItem animated:NO];
[self.view addSubview:navigationBar];
[navigationBar release];
[backButton release];