覆盖 iOS 中的“后退”按钮时出现问题

发布于 2024-12-03 11:58:15 字数 655 浏览 1 评论 0原文

我有一个 UITableViewController A 正在将 UITableViewController B 推入堆栈。

在 A 中,我有代码:

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Trending" 
                                                                                     style:UIBarButtonItemStylePlain 
                                                                                    target:self 
                                                                                    action:@selector(backButtonClicked:)];

backButtonClicked: 也已实现。

B 的标题是 Trending,但是当我单击它时,它永远不会到达 backButtonClicked:

这是为什么?

I have a UITableViewController A that is pushing UITableViewController B onto the stack.

In A i have the code:

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Trending" 
                                                                                     style:UIBarButtonItemStylePlain 
                                                                                    target:self 
                                                                                    action:@selector(backButtonClicked:)];

backButtonClicked: is also implemented.

B has the title Trending, but when I click it, it doesn't ever reach backButtonClicked:

Why is this?

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

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

发布评论

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

评论(3

伴我心暖 2024-12-10 11:58:15

尝试设置委托:

[navigationController setDelegate:self];

或使用左按钮。有时后退按钮不适用于某些视图,我不得不使用左按钮:

self.navigationItem.leftBarButtonItem =  [[UIBarButtonItem alloc] initWithTitle:@"Trending" style:UIBarButtonItemStylePlain target:self action:@selector(backButtonClicked:)];

另外,您可以尝试设置 B 的按钮项而不是 A:

viewControllerB.navigationItem.backBarButtonItem =  [[UIBarButtonItem alloc] initWithTitle:@"Trending" style:UIBarButtonItemStylePlain target:self action:@selector(backButtonClicked:)];

Try either setting the delegate:

[navigationController setDelegate:self];

Or using the left button. Sometimes the back button doesn't work with certain views and I have had to use the left button instead:

self.navigationItem.leftBarButtonItem =  [[UIBarButtonItem alloc] initWithTitle:@"Trending" style:UIBarButtonItemStylePlain target:self action:@selector(backButtonClicked:)];

Also, you can try setting B's button item instead of A:

viewControllerB.navigationItem.backBarButtonItem =  [[UIBarButtonItem alloc] initWithTitle:@"Trending" style:UIBarButtonItemStylePlain target:self action:@selector(backButtonClicked:)];
陌路黄昏 2024-12-10 11:58:15

在 Xcode 文档中,它指出 backBarButtonItem 目标和操作应设置为 nil。因此,即使您确实设置了它,它也可能会被忽略。您可以查看下面的链接,将自定义行为添加到后退按钮。

后退按钮项上的自定义操作

或者您可以执行以下操作in viewControllerB:

self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Trending" 
                                                                             style:UIBarButtonItemStylePlain 
                                                                            target:self
                                                                            action:@selector(backButtonClicked:)] autorelease];

然后也将其添加到 viewControllerB

- (void)backButtonClicked:(id)sender {

    [[[self.navigationController viewControllers] objectAtIndex:0] backButtonClicked:sender];

    [self.navigationController popViewControllerAnimated:YES];

}

上面的方法将找到 RootViewController 并向其发送 backButtonClicked 消息。然后它将弹出当前视图控制器,这应该允许您模拟 backBarButtonItem。您还可以通过更改 objectAtIndex 方法中的值来更改要发送消息的视图控制器。

In the Xcode documentation, it states that the backBarButtonItem target and action should be set to nil. So even if you do set it, it's probably a good bet that it will be ignored. You could check out the link below to add custom behaviour to the back button.

Custom Action on Back Button Item

Or you could just do the following in viewControllerB:

self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Trending" 
                                                                             style:UIBarButtonItemStylePlain 
                                                                            target:self
                                                                            action:@selector(backButtonClicked:)] autorelease];

Then also add this to viewControllerB

- (void)backButtonClicked:(id)sender {

    [[[self.navigationController viewControllers] objectAtIndex:0] backButtonClicked:sender];

    [self.navigationController popViewControllerAnimated:YES];

}

The above method will find the RootViewController and send it the backButtonClicked message. It will then pop the current view controller, which should allow you to emulate the backBarButtonItem. Also you can change which view controller you want to send the message by changing the value in the objectAtIndex method.

蘑菇王子 2024-12-10 11:58:15

尝试不要使用 backBarButtonItem 而是 leftBarButtonItem,

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Trending" style:UIBarButtonItemStylePlain target:self action:@selector(backButtonClicked:)];
self.navigationItem.hidesBackButton=YES;

对我来说它就像一个魅力。并且不要忘记您没有释放此按钮 - 这可能会导致内存泄漏。因此,您可以在分配按钮时添加自动释放或使其像这样

UIBarButtonItem *myBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Trending" style:UIBarButtonItemStylePlain target:self action:@selector(backButtonClicked:)];
self.navigationItem.leftBarButtonItem = myBackButton;
[myBackButton release];
self.navigationItem.hidesBackButton=YES;

try not backBarButtonItem but leftBarButtonItem instead

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Trending" style:UIBarButtonItemStylePlain target:self action:@selector(backButtonClicked:)];
self.navigationItem.hidesBackButton=YES;

for me it works like a charm. And don't forget that you didn't release this button - and it can cause memory leak. So you can add autorelease when you assign you button or make it like this

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