如何更改 UINavigationControler 上的后退按钮

发布于 2024-09-30 21:01:39 字数 458 浏览 8 评论 0原文

在视图中,我想更改它,因为我有以下代码,但它失败了。

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //Logout button
    UIBarButtonItem * logout = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(goBack)];
    logout.title = @"Logout";
    nav_delegate.navigationController.navigationItem.leftBarButtonItem = logout;
    [logout release];
}

感谢您的帮助。

In the view I want to change it for I have the following code but it fails.

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //Logout button
    UIBarButtonItem * logout = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(goBack)];
    logout.title = @"Logout";
    nav_delegate.navigationController.navigationItem.leftBarButtonItem = logout;
    [logout release];
}

Thank you for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

放低过去 2024-10-07 21:01:39

实现 backBarButtonItem 是针对使用pushViewController:subViewController 的超级视图控制器。

例如,如果您为其超级视图控制器 Logout 推送了一个视图控制器:

[self.navigationController pushViewController:subViewController animated:YES];

那么,您应该在超级视图(即 Logout 视图)中实现 backBarButtonItem,不在推送的子ViewController 中

因此,要实现 backBarButtonItem,您可以在超级视图 Logout 中执行此操作,例如:

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:logoutViewTitle style:UIBarButtonItemStyleBordered target:nil action:nil];

您可以在 -(void)viewDidLoad 中执行此操作以进行静态使用,或在 -(void)viewWillAppear:(BOOL)animated 中执行此操作以进行动态使用,用于设置标题无需分配和初始化。

还有一点提示:在界面生成器中,有一个 backBarButtonItem 标题的输入字段。但如果你没有输入,你必须在.m文件中分配并初始化带有title的backBarButton,如上面的代码。如果您输入了静态使用的标题,我相信您可以简单地通过使用来更改它:

[self.navigationItem.backBarButtonItem setTitle:logoutViewTitle];

希望它有帮助。

Implementing backBarButtonItem is for the super view controller which uses pushViewController:subViewController.

For example, if you've pushed a view controller for its super view controller Logout:

[self.navigationController pushViewController:subViewController animated:YES];

Then, you should've implemented backBarButtonItem in the super view, which is Logout view, NOT in the pushed subViewController.

So, to implement backBarButtonItem, you do it in super view Logout, like:

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:logoutViewTitle style:UIBarButtonItemStyleBordered target:nil action:nil];

You can do this in -(void)viewDidLoad for static use, or in -(void)viewWillAppear:(BOOL)animated for dynamic use, for setting the title without allocating and initializing.

One more tip: In interface builder, there is input field for backBarButtonItem title. But if you didn't enter, you must allocate and initialize the backBarButton with title in .m files, like the code above. If you've entered the title for static use, I believe you can change it simply by using:

[self.navigationItem.backBarButtonItem setTitle:logoutViewTitle];

Hope it helped.

魂ガ小子 2024-10-07 21:01:39

在前一个视图控制器(按下后退按钮时将返回到的视图控制器)上设置 backBarButtonItem

Set the backBarButtonItem on the previous view controller (the one that you will return to when you press the back button).

乱了心跳 2024-10-07 21:01:39

答案如下。在视图控制器中:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"X";
    //Logout button
    UIBarButtonItem * logout = [[UIBarButtonItem alloc] initWithTitle:@"Logout" style: UIBarButtonItemStylePlain target:self action:@selector(goBack)];
    self.navigationItem.leftBarButtonItem = logout;
    [logout release];
}

Here's the answer. In the view controller:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"X";
    //Logout button
    UIBarButtonItem * logout = [[UIBarButtonItem alloc] initWithTitle:@"Logout" style: UIBarButtonItemStylePlain target:self action:@selector(goBack)];
    self.navigationItem.leftBarButtonItem = logout;
    [logout release];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文