xcode UINavigationController 不显示后退按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12961345/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:51:55  来源:igfitidea点击:

UINavigationController not showing back button

iphoneobjective-ciosxcodeipad

提问by LilMoke

I have a storyboard with a UIViewController with some buttons. One of the buttons does a modal segue to a UINavigationController and the NavController has a UITableViewController embedded in it. When I click the button on the home screen it advances to the NavController but there is not back button.

我有一个带有一些按钮的 UIViewController 的故事板。其中一个按钮对 UINavigationController 进行模式切换,NavController 中嵌入了 UITableViewController。当我单击主屏幕上的按钮时,它会前进到 NavController,但没有后退按钮。

So how do I get a back button? I have tried a few things, but no luck.

那么如何获得后退按钮呢?我尝试了几件事,但没有运气。

Thanks for the help!!

谢谢您的帮助!!

回答by ChrisH

If you are presenting the navigation controller modally, then the tableview controller is the only view controller your new navigation controller has pushed. There would not be and should not be a back button in that case.

如果您以模态方式呈现导航控制器,那么 tableview 控制器是您的新导航控制器推送的唯一视图控制器。在这种情况下不会也不应该有后退按钮。

You'd be better off adding a cancel/done button to the nav bar via the tableview controller, which dismisses the modal view.

您最好通过 tableview 控制器向导航栏添加一个取消/完成按钮,这会关闭模态视图。

In your tableView controller viewDidLoad:method:

在您的 tableView 控制器viewDidLoad:方法中:

UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonTapped:)];
self.navigationItem.leftBarButtonItem = done;

//Release done if not using ARC

Then add (the simplest implementation of) a dismiss method:

然后添加(最简单的实现)一个dismiss方法:

- (void)doneButtonTapped:(id)sender {

    [self.navigationController dismissViewControllerAnimated:YES completion:nil];

}

回答by Matias

A UINavigationViewController only manages a stack of view controllers. You push a viewController onto the stack, and when you hit "back" you are popping a view off of the stack.

UINavigationViewController 只管理一堆视图控制器。您将一个 viewController 推入堆栈,当您点击“返回”时,您将从堆栈中弹出一个视图。

Since you are presenting the UINavigationController modally, it has no knowledge of what was presented before it. The correct way to get the desired behavior is to set your main UIViewController to the UINavigationController's root view controller. When the user taps a button, you push the new UITableViewController onto the UINavigationController's stack.

由于您以模态方式呈现 UINavigationController,因此它不知道在它之前呈现的内容。获得所需行为的正确方法是将主 UIViewController 设置为 UINavigationController 的根视图控制器。当用户点击一个按钮时,你将新的 UITableViewController 推送到 UINavigationController 的堆栈上。

回答by bpapa

Your UITableViewController subclass is the root view controller of the UINavigationController. Therefore, a back button will not be displayed automatically. If tapping on a table row pushes a second View Controller onto the stack, then that second View Controller will display a back button in its Navigation Bar. So, this is the expected behavior.

你的 UITableViewController 子类是 UINavigationController 的根视图控制器。因此,不会自动显示后退按钮。如果点击表格行将第二个视图控制器推送到堆栈中,那么第二个视图控制器将在其导航栏中显示一个后退按钮。所以,这是预期的行为。

You can add a "back" button yourself by creating one (you'd need an image that looks like a back button, or draw one in code) and add it as the leftBarButtonItem of the TableViewController's navigationItem, but I wouldn't do that. A View Controller presented modally shouldn't go "back". The button instead should be something like "Close", "Dismiss", or "Cancel".

您可以通过创建一个“后退”按钮来自己添加一个“后退”按钮(您需要一个看起来像后退按钮的图像,或者在代码中绘制一个)并将其添加为 TableViewController 的导航项的 leftBarButtonItem,但我不会这样做. 模态呈现的视图控制器不应该“返回”。该按钮应该类似于“关闭”、“关闭”或“取消”。

回答by JanB

In my case, I had a rootviewcontoller that didn't have a visible navigation bar but the pushed viewcontollers all needed a navigation bar with a back button but they weren't showing. I'm working in Xcode 6.1. In the viewDidAppear for the rootviewcontroller, I set:

就我而言,我有一个 rootviewcontoller,它没有可见的导航栏,但推送的 viewcontoller 都需要一个带有后退按钮的导航栏,但它们没有显示。我在 Xcode 6.1 中工作。在 rootviewcontroller 的 viewDidAppear 中,我设置了:

self.title=@"a title"; self.navigationController.navigationBarHidden=YES;

self.title="一个标题"; self.navigationController.navigationBarHidden=YES;

In the viewDidLoad for the pushed view controllers that need the navigation bar to be visible, I set: self.navigationController.navigationBarHidden=NO; self.navigationItem.hidesBackButton=NO;

在需要导航栏可见的推送视图控制器的 viewDidLoad 中,我设置: self.navigationController.navigationBarHidden=NO; self.navigationItem.hidesBackButton=NO;

It all seems a bit obvious but I was going round in circles trying to show the navigation bar with the back button!

这一切似乎有点明显,但我一直在绕圈子试图显示带有后退按钮的导航栏!