xcode 如何在 UINavigationController 的 RootViewController 上显示后退按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11283218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to show back button on the RootViewController of the UINavigationController?
提问by shaikh
Here is my code. I want to put a back button on the opening rootviewController.
这是我的代码。我想在打开的 rootviewController 上放一个后退按钮。
- (void)selectSurah:(id)sender {
SurahTableViewController * surahTableViewController = [[SurahTableViewController alloc] initWithNibName:@"SurahTableViewController" bundle:nil];
surahTableViewController.navigationItem.title=@"Surah";
surahTableViewController.navigationItem.backBarButtonItem.title=@"Back";
UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:surahTableViewController];
[self presentModalViewController:aNavigationController animated:YES];
}
采纳答案by CodaFi
I don't believe it's possible to pop the root view controller off the navigation stack, but you can fake it with a UIButton
added as the custom view of a UIBarButtonItem
:
我不相信从导航堆栈中弹出根视图控制器是可能的,但是您可以通过UIButton
添加作为 a 的自定义视图来伪造它UIBarButtonItem
:
UIButton *b = [[UIButton alloc]initWithButtonType:UIButtonTypeCustom];
[b setImage:[UIImage imageNamed:@"BackImage.png"] forState:UIControlStateNormal];
[b addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
self.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:b];
A suitable PSD of iOS UI elements can be found here.
可以在此处找到合适的 iOS UI 元素 PSD 。
回答by Lorenzo B
Faizan,
法赞,
Helium3comment makes sense.
Helium3 的评论是有道理的。
I suppose that your button is needed to dismiss the controller presented modally, is it true? Correct if I'm wrong.
我想你的按钮需要关闭模态呈现的控制器,是真的吗?如果我错了,请纠正。
If so, you could just create a new UIBarButtonItem
and set is a left (or right) button for the UINavigationController
navigationItem
. To not break encapsulation create it in the viewDidLoad
method for your SurahTableViewController
controller.
如果是这样,您可以创建一个新的UIBarButtonItem
并设置为UINavigationController
navigationItem
. 为了不破坏封装,请在控制器的viewDidLoad
方法中创建它SurahTableViewController
。
- (void)viewDidLoad
{
[super viewDidLoad];
// make attention to memory leak if you don't use ARC!!!
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Close"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(close:)];
}
-(void)close:(id)sender
{
// to dismiss use dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion
// dismissModalViewControllerAnimated: is deprecated
[self dismissViewControllerAnimated:YES completion:^{ NSLog(@"controller dismissed"); }];
}
回答by Jamie
Since the SurahTableViewController
is a root view controller in a navigation controller you can't go back to the root because you're already there. Since you've presented it modally from something else, you need to put a button on the nav bar that has an IBAction
which calls:
由于SurahTableViewController
是导航控制器中的根视图控制器,因此您无法返回根视图,因为您已经在那里了。由于您已经从其他东西模态地呈现它,您需要在导航栏上放置一个按钮,该按钮具有以下IBAction
调用:
[self dismissModalViewControllerAnimated:YES];
回答by Andy Obusek
Appearance and behavior of a back button in a UINavigationController relies on interaction between a stack of UINavigationControllers. Putting a back button on the first controller breaks this convention, there's nothing to go back to, which is why your code isn't working.
UINavigationController 中后退按钮的外观和行为依赖于 UINavigationController 堆栈之间的交互。在第一个控制器上放一个后退按钮打破了这个约定,没有什么可以返回的,这就是为什么你的代码不起作用的原因。
You'll need to manually add UIBarButtonItem to the title bar code like:
您需要手动将 UIBarButtonItem 添加到标题栏代码中,例如:
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
If you truly want it to look like a back button, you'll need to manually create the UIBarButtonItem with an image that mirrors the back button.
如果你真的想让它看起来像一个后退按钮,你需要手动创建 UIBarButtonItem 和一个镜像后退按钮的图像。
Another suggestion though, as it looks like you are attempting to use a back button to dismiss a modal view controller, I'd stick with something more conventional like a "Close" or "Done" button to close the modal view controller. A back button is really more appropriate for navigating a stack of UINavigationControllers.
不过,另一个建议是,由于您似乎正在尝试使用后退按钮来关闭模态视图控制器,因此我会坚持使用更传统的方法,例如“关闭”或“完成”按钮来关闭模态视图控制器。后退按钮确实更适合导航 UINavigationControllers 堆栈。