【问题标题】:UINavigation bar's title and rightbar button is displayed in all the view controllers I haveUI 导航栏标题和右栏按钮显示在我拥有的所有视图控制器中
【发布时间】:2016-06-02 06:33:53
【问题描述】:

我有一个带有情节提要和 2 个视图控制器的应用。我已经通过 Appdelegate 以编程方式创建了导航控制器。我在 Viewcontroller1 的viewDidLoad() 中将标题设置为“Screen1”,并添加了一个右栏按钮。

现在,我的问题是,当我单击右栏按钮时,会显示 viewcontroller2,标题设置为“Screen1”,并且右栏按钮也会出现。

注意:在 ViewController2 中,我没有设置任何标题,也没有添加任何条形按钮。

我的代码:

AppDelegate.m: -> didFinishLaunchingWithOptions():

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    
VC1 = [[ViewController1 alloc] init];
self.navController = [[UINavigationController alloc] initWithRootViewController:self.VC1];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];

ViewController1.m -> didViewLoad():

self.navigationItem setTitle:@"first slide"];

UIBarButtonItem *goto2 = [[UIBarButtonItem alloc] initWithTitle:@"Goto to 2" style:UIBarButtonItemStylePlain target:self action:@selector(GotoSlide2)];
[self.navigationItem setRightBarButtonItem:goto2];


-(void) GotoSlide2
{
ViewController2 *vc2 = [[ViewController2 alloc] init];
[self.navigationController pushViewController:vc2 animated:YES];

}

【问题讨论】:

    标签: ios objective-c iphone uinavigationbar


    【解决方案1】:

    如果要在 ViewController2 中隐藏导航栏,请在 ViewController2 的viewDidLoad 中编写以下代码:

    self.navigationController.navigationBarHidden=YES;
    

    它将为 ViewController2 隐藏 navigationBar

    编辑: 要隐藏标题,请在 ViewController2 的 viewDidLoad 中编写以下代码:

    self.navigationItem.titleView = [[UIView alloc] initWithFrame:CGRectZero];
    

    UILabel *label = [[UILabel alloc] init];
    self.navigationItem.titleView = label;
    

    来自这个答案:https://stackoverflow.com/a/2461163/5575752

    隐藏右栏按钮:

    self.navigationItem.rightBarButtonItem.title = @"";
    self.navigationItem.rightBarButtonItem.enabled = NO;
    

    【讨论】:

    • 不,我不想隐藏导航栏。我希望标题为空,并且不应在 ViewController2 中显示右栏按钮。
    • 好的。要隐藏标题和右栏按钮,这很好。但我想知道它为什么显示在 ViewController2 中。我正在创建导航控制器并在代码中设置它的 rootviewcontroller,而不是使用情节提要。我想知道,这是我在代码中完成所有操作的方式吗?
    • 是的,所有视图控制器都有它的导航控制器和来自应用委托的导航栏,因为您将导航控制器设置为根视图控制器@vinoth
    • 我很好,所有的视图控制器都从 AppDelegate 获取导航控制器和栏,但是为什么标题和按钮都存在于所有视图控制器中,而我只在 ViewController1 中添加了这些? @罗纳克
    • 导航栏始终存在并由导航控制器本身管理,导航控制器使用导航堆栈上的视图控制器提供的内容更新导航栏。阅读更多:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationController_Class/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多