ios - UI 导航栏标题和右栏按钮显示在我拥有的所有 View Controller 中

标签 ios objective-c iphone uinavigationbar

我有一个带有 Storyboard和 2 个 View Controller 的应用程序。我已经通过 Appdelegate 以编程方式创建了导航 Controller 。我在 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];

}

最佳答案

如果想在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;

关于ios - UI 导航栏标题和右栏按钮显示在我拥有的所有 View Controller 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37584336/

相关文章:

ios - 如何在 Swift 中调用 Objective-C instancetype 方法?

ios - 将 nsstring 转换成 pdf 格式然后发送邮件

ios - Xcode 横向多个 Storyboard

iphone - 在 Restkit 中处理 NSAsserts 抛出

ios - 快速解析来自rest api的xml数据

ios - 访问电池供电的 iBeacon 设备(主要是 kontakt io 设备)的电池电量?

ios - 从 firebase 数据库 ios 检索数据的问题

ios - 如何在 JTCalendar Objective-C 中以 "MM/yyyy"格式获取当前显示的月份和年份

iphone - 带参数的选择器

ios - swift 3.0 和 iOS : How to set the delete privileges of a file in Swift