navigationcontroller标题和按钮

时间:2013-02-06 06:16:56

标签: iphone ios6 uinavigationcontroller

在基于选项卡的应用程序委托中,我添加了导航控制器,如下所示

 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
 UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
 UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

 UIViewController *viewController3 = [[view1 alloc] initWithNibName:@"view1" bundle:nil];
 UIViewController *viewController4 = [[view2 alloc] initWithNibName:@"view2" bundle:nil];
 UIViewController *viewController5 = [[view3 alloc] initWithNibName:@"view3" bundle:nil];
 self.tabBarController = [[UITabBarController alloc] init];
 self.tabBarController.viewControllers = @[viewController1, viewController2,viewController3,viewController4,viewController5];
 navigationController=[[UINavigationController alloc]initWithRootViewController:self.tabBarController];
 [self.window addSubview:[navigationController view]];
self.window.rootViewController = self.navigationController;

我无法在每个视图中添加标题。虽然添加它没有显示标题?请帮我解决这个问题

5 个答案:

答案 0 :(得分:1)

使用以下代码...

在此我向每个标签添加UINavigationController,同时为标签指定标题,也为UINavigationBar指定如下标题...

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    UIViewController *viewController1, *viewController2, *viewController3, *viewController4, *viewController5;
    UINavigationController *navviewController1 , *navviewController2, *navviewController3, *navviewController4, *navviewController5;
    self.tabBarController = [[[UITabBarController alloc] init] autorelease];
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        viewController1 = [[[YourViewController1 alloc] initWithNibName:@"YourViewController1" bundle:nil] autorelease];
        navviewController1=[[UINavigationController alloc]initWithRootViewController:viewController1];
        navviewController1.title = @"Home";
        //        navviewController1.navigationBarHidden=YES;

        viewController2 = [[[YourViewController2 alloc] initWithNibName:@"YourViewController2" bundle:nil] autorelease];
        navviewController2=[[UINavigationController alloc]initWithRootViewController:viewController2];
        //        navviewController2.navigationBarHidden=YES;
        navviewController2.title = @"HowItsWork";

        viewController3 = [[[YourViewController3 alloc] initWithNibName:@"YourViewController3" bundle:nil] autorelease];
        navviewController3=[[UINavigationController alloc]initWithRootViewController:viewController3];
        //        navviewController3.navigationBarHidden=YES;
        navviewController3.title = @"Join Us";


        viewController4 = [[[YourViewController4 alloc] initWithNibName:@"YourViewController4" bundle:nil] autorelease];
        navviewController4=[[UINavigationController alloc]initWithRootViewController:viewController4];
        //        navviewController4.navigationBarHidden=YES;    
        navviewController4.title = @"Become";  

        viewController5 = [[[YourViewController5 alloc] initWithNibName:@"YourViewController5" bundle:nil] autorelease];
        navviewController5=[[UINavigationController alloc]initWithRootViewController:viewController5];
        //        navviewController4.navigationBarHidden=YES;    
        navviewController5.title = @"Contact Us"; 
        self.tabBarController.viewControllers = [NSArray arrayWithObjects:navviewController1, navviewController2,navviewController3,navviewController4,navviewController5, nil];
        self.window.rootViewController = self.tabBarController;
    }

    [self.window makeKeyAndVisible];
}

<强> 2)OR

此外,您可以在特定的课程中简单地指定标题,如下面的...

-(void)viewWillAppear:(BOOL)animated{
    self.title = @"yourTitle";
}

我希望这对你有帮助......

答案 1 :(得分:0)

转到每个标签栏控制器中的viewdidload方法&amp;使用行:

navigationController.title=@"hello";

答案 2 :(得分:0)

在每个viewcontroller中都有viewdidload      self.navigationcontroler.title = @ “firstVC”;

答案 3 :(得分:0)

“title”是UIViewController的属性,而UINavigationControllerUIViewController的子类,您也可以访问UINavigationController的对象,就像navviewController.title = @"myTitle";一样有关详细信息,请查看Apple Docs。希望它会有所帮助。干杯

答案 4 :(得分:0)

您已在导航控制器中添加了TabBar控制器,因此您无法添加标题并出现此问题。

尝试使用Tabbar控制器内的View控制器添加每个导航控制器。 我的意思是将你的ViewController添加为导航控制器的根视图控制器,然后添加到标签栏控制器。

viewController1 = [[[YourViewController1 alloc] initWithNibName:@"YourViewController1" bundle:nil] autorelease];
        navviewController1=[[UINavigationController alloc]initWithRootViewController:viewController1];

现在将此导航控制器内部广告到tabbar控制器。为所有其他viewcontrollers执行此操作