3

我想在 navigationItem 中自定义 backBarButtonItem,这是我的代码

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                             style:UIBarButtonItemStylePlain
                                                            target:nil
                                                            action:nil];
    [[self navigationItem] setBackBarButtonItem:back];
}

但是当我使用 leftBarButtionItem 时它不起作用。

不知道,谁能给我答案?

4

3 回答 3

1
- (void)viewDidLoad
{
    [self.navigationItem setHidesBackButton:YES];

    UIBarButtonItem *cancelNavButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(dismissController)];
    [self.navigationItem setLeftBarButtonItem:cancelNavButton];
}

- (void)dismissController
{
    [self.navigationController popViewControllerAnimated:YES];
}
于 2014-11-13T14:32:37.557 回答
0

如果您使用UINavigationController'pushViewController:animated:方法推送视图控制器,则不必自己创建后退按钮。这会将新的 ViewController 推送到 NavigationControllers 堆栈上,并为您提供一个带有前一个 ViewController 标题的自动后退按钮。

您的按钮不起作用的原因是target:action:参数,nil您可以将它们绑定到UINavigationController'popViewControllerAnimated:方法以实现与默认后退按钮相同的操作。

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                     style:UIBarButtonItemStylePlain
                                                    target:[self navigationController]
                                                    action:@selector(popViewControllerAnimated:)
                                                     ];
    [[self navigationItem] setBackBarButtonItem:back];

}
于 2013-09-23T02:17:36.220 回答
0
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.hidesBackButton = YES;
    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                             style:UIBarButtonItemStylePlain
                                                            target:nil
                                                            action:nil];
    self.navigationItem.leftBarButtonItem = back;
}
于 2013-09-23T05:10:27.610 回答