ios 导航项未出现在视图控制器上
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10891019/
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
Navigation Item not appearing on View Controller
提问by SRQMarsh
I apologize I am new to iphone programming.
我很抱歉我是 iphone 编程的新手。
I have created a Master-Detail Iphone application (so Navigation Controller came with the project). I segue to a new view controller I created through a UIBarButtonItem on the masterviewcontroller. However unlike the detailviewcontroller (that came with the project) I can not seem to get the navigationitem (or navigationbar?) to display on the view even though it appears in the scene list of my storyboard.
我创建了一个 Master-Detail Iphone 应用程序(因此导航控制器随项目一起提供)。我转而使用我通过 masterviewcontroller 上的 UIBarButtonItem 创建的新视图控制器。但是,与 detailviewcontroller(项目附带的)不同,即使导航项(或导航栏?)出现在我的故事板的场景列表中,我似乎也无法在视图上显示它。
Heres some code and a screenshot:
这是一些代码和屏幕截图:
In my masterviewcontroller.m viewdidload() function
在我的 masterviewcontroller.m viewdidload() 函数中
UIBarButtonItem *settingsButton = [[UIBarButtonItem alloc] initWithTitle:@"Settings" style:normal target:self action:@selector(goToSettings:)];
self.navigationItem.leftBarButtonItem = settingsButton;
in my masterviewcontroller.m
在我的 masterviewcontroller.m 中
- (IBAction)goToSettings:(id)sender{
[self performSegueWithIdentifier:@"SettingsSegue" sender:self];
}
I tried adding a title to the navigationitem during the viewDidLoad function of the new viewcontroller.m class i created (mentioned in this Linkbut it didn't work)
我尝试在我创建的新 viewcontroller.m 类的 viewDidLoad 函数期间为导航项添加标题(在此链接中提到但它没有用)
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationItem.title = @"Settings";
self.navigationItem.leftBarButtonItem = self.editButtonItem;
}
But it still shows up in my storyboard ( it shows up in the list under the scene but not in the display of the view)
但它仍然出现在我的故事板中(它出现在场景下的列表中,但不在视图的显示中)
So my question is why is it now showing up and how do I get it to? I want a back button like my detail view controller that came with the master-detail project.
所以我的问题是为什么它现在出现了,我如何才能得到它?我想要一个像我的细节视图控制器一样的后退按钮,它是主从项目附带的。
EDIT#1
编辑#1
I have added a check for whether navigation controller is nil and it is not nil (the if statement is never entered) I also tried changing the navigation item to back and removing and none has worked.
我添加了一个检查导航控制器是否为 nil 并且它不是 nil (从未输入 if 语句)我还尝试将导航项更改为 back 并删除,但没有任何效果。
if(self.navigationItem == nil)
{
[ self.navigationItem init];
}
self.navigationItem.title = @"Settings";
self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem;
Now that I have enough reputation to show an image I can show that the navigation item shows up in the list but doesnt show up on the view
现在我有足够的声誉来显示图像,我可以显示导航项显示在列表中但没有显示在视图中
采纳答案by SRQMarsh
The Navigation item did not show up because of the "style" of my segue.
由于我的 segue 的“风格”,导航项没有出现。
The segue that moved the scene from the master view controller to the settings view controller was set to 'modal'. It has to be set to 'push'. This is done from the storyboard on the utilities pane
将场景从主视图控制器移动到设置视图控制器的 segue 设置为“模态”。它必须设置为“推”。这是从实用程序窗格上的故事板完成的
回答by Tchelow
You have to change your Segue Style by doing this:
您必须通过执行以下操作来更改您的 Segue 样式:
- Select the Segue in your Storyboard;
- Go to the Attributes inspector (on the right pane tabs);
- Change the Style attribute to Push.
- 在故事板中选择 Segue;
- 转到属性检查器(在右侧窗格选项卡上);
- 将样式属性更改为推送。
It will make the screen roll from right to left and the Navigation bar will appear sliding. With Modal, the screen come from bottom to the top of the screen and it will not let the Navigation bar appears.
它将使屏幕从右向左滚动,导航栏将出现滑动。使用 Modal,屏幕从屏幕底部到顶部,它不会让导航栏出现。
回答by Jorge Casariego
The problem in my case was that the Navigation controller wasn't the "initial View Controller"
我的问题是导航控制器不是“初始视图控制器”
So I had to do this
所以我不得不这样做
回答by heckman
You have verified that your navigation controller listed in the viewDidLoad method is not nil?
您已确认 viewDidLoad 方法中列出的导航控制器不为零?
Assuming that you do correctly have a self.navigationItem
that is non-nil, it appears that you are incorrectly setting your self.navigationItem.leftBarButtonItem
to an editButtonItem
, instead try removing this all together!
假设您确实有一个self.navigationItem
非零的a ,那么您似乎错误地将您的 设置self.navigationItem.leftBarButtonItem
为 an editButtonItem
,而是尝试将其全部删除!
If removing it does not solve the problem try:
如果删除它不能解决问题,请尝试:
self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem;
回答by Pradeep Reddy Kypa
Please check if your view controller is the root view controller of the navigation controller. You cant directly present a view controller as a modal view controller. You have to make your view controller as the root view controller of a navigation controller and then present your navigation controller as the modal view controller.
请检查您的视图控制器是否是导航控制器的根视图控制器。您不能直接将视图控制器呈现为模态视图控制器。您必须将您的视图控制器作为导航控制器的根视图控制器,然后将您的导航控制器呈现为模态视图控制器。
// Create the root view controller for the navigation controller
// The new view controller configures a Cancel and Done button for the
// navigation bar.
YourViewController *addController = [[YourViewController alloc]
init];
// Configure the YourViewController.
// Create the navigation controller and present it.
UINavigationController *navigationController = [[UINavigationController alloc]
initWithRootViewController:addController];
[self presentViewController:navigationController animated:YES completion: nil];
回答by Dan Alboteanu
You need to have a Navigation Controllerand a View Controller.
您需要有一个导航控制器和一个视图控制器。
"The navigation controller manages the navigation bar at the top of the interface and an optional toolbar at the bottom of the interface. The navigation bar is always present and is managed by the navigation controller itself, which updates the navigation bar using the content provided by its child view controllers. When the isToolbarHiddenproperty is false, the navigation controller similarly updates the toolbar with contents provided by the topmost view controller." https://developer.apple.com/documentation/uikit/uinavigationcontroller
“导航控制器管理界面顶部的导航栏和界面底部的可选工具栏。导航栏始终存在并由导航控制器本身管理,它使用由提供的内容更新导航栏它的子视图控制器。当isToolbarHidden属性为 false 时,导航控制器类似地使用最顶层视图控制器提供的内容更新工具栏。” https://developer.apple.com/documentation/uikit/uinavigationcontroller