覆盖 iOS 中的“后退”按钮时出现问题
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试设置委托:
或使用左按钮。有时后退按钮不适用于某些视图,我不得不使用左按钮:
另外,您可以尝试设置 B 的按钮项而不是 A:
Try either setting the delegate:
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:
Also, you can try setting B's button item instead of A:
在 Xcode 文档中,它指出 backBarButtonItem 目标和操作应设置为 nil。因此,即使您确实设置了它,它也可能会被忽略。您可以查看下面的链接,将自定义行为添加到后退按钮。
后退按钮项上的自定义操作
或者您可以执行以下操作in viewControllerB:
然后也将其添加到 viewControllerB
上面的方法将找到 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:
Then also add this to viewControllerB
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.
尝试不要使用 backBarButtonItem 而是 leftBarButtonItem,
对我来说它就像一个魅力。并且不要忘记您没有释放此按钮 - 这可能会导致内存泄漏。因此,您可以在分配按钮时添加自动释放或使其像这样
try not backBarButtonItem but leftBarButtonItem instead
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