简体   繁体   English

切换到另一个视图时显示导航栏

[英]displaying navigation bar when switching to another view

I have a button when it pressed, I want it to take me to another view (the "news" view). 我按下按钮时有一个按钮,希望它带我到另一个视图(“新闻”视图)。 Within the news view, I want there to be a navigation bar with a back button. 在新闻视图中,我希望有一个带有后退按钮的导航栏。 I have a navigationcontroller setup throughout my app but I can't seem to get this to work when this button is pressed. 我在整个应用程序中都有一个Navigationcontroller设置,但是当按下此按钮时,似乎无法正常工作。 It takes me to the view I want but there is no navigation bar and no back button. 它带我到所需的视图,但是没有导航栏和后退按钮。 This is my code that is implemented when the button is pressed. 这是我在按下按钮时实现的代码。

If anybody know what I am doing wrong, it would be much appreciated. 如果有人知道我在做什么错,将不胜感激。

Thanks 谢谢

-(IBAction)news
{
newsViewController *view1 = [[newsViewController alloc] initWithNibName:@"newsViewController" bundle:nil];
view1.title=@"news";
[self.navigationController pushViewController:view1 animated:YES];
}

I am not in my mac, so I can not test code, but if it is working and the only issue you got is not show the bar, what you need to is set the bar to be visible: 我不在Mac上,因此无法测试代码,但是如果它可以正常工作,并且您遇到的唯一问题是不显示该栏,则需要将该栏设置为可见:

From apple docs: 从苹果文档:

The navigation toolbar is hidden by default but you can show it for your navigation interface by calling the setToolbarHidden:animated: method of your navigation controller object. 导航工具栏默认情况下是隐藏的,但是您可以通过调用导航控制器对象的setToolbarHidden:animated:方法在导航界面中显示它。 If not all of your view controllers support toolbar items, your delegate object can call this method to toggle the visibility of the toolbar during subsequent push and pop operations. 如果不是所有的视图控制器都支持工具栏项目,则委托对象可以调用此方法以在后续的推入和弹出操作期间切换工具栏的可见性。

Something like that is supposed to work: 这样的事情应该起作用:

-(IBAction)news {
    newsViewController *view1 = [[newsViewController alloc] initWithNibName: @"newsViewController" bundle:nil];
    view1.title=@"news";

    [self.navigationController pushViewController:view1 animated:YES];

    //Add this line!
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

I hope it can help you. 希望对您有所帮助。

write the below code in page where you want to show navigation controller 
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM