【问题标题】:How to add UITabbar controller and UINavigation bar on UIViewController in Objective C如何在Objective C中的UIViewController上添加UITabbar控制器和UINavigation bar
【发布时间】:2016-12-22 12:18:36
【问题描述】:

我是 iOS 新手,在 UIViewController 上添加 UITabbarControllerUINavigationController 时遇到问题。我尝试这样的代码

在 AppDeleagte.m 中

ViewController1 *news=[[ViewController1 alloc]initWithNibName:@"ViewController1" bundle:nil ];
ViewController2 *menu =[[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];
ViewController3 *emergency =[[ViewController3 alloc] initWithNibName:@"ViewController3" bundle:nil];
ViewController4 *calender=[[ViewController4 alloc]initWithNibName:@"ViewController4" bundle:nil];
ViewController5 *help =[[ViewController5 alloc] initWithNibName:@"ViewController5" bundle:nil];
UINavigationController *nav1=[[UINavigationController alloc]initWithRootViewController:menu];
UINavigationController *nav2=[[UINavigationController alloc]initWithRootViewController:emergency];
UINavigationController *nav3=[[UINavigationController alloc]initWithRootViewController:calender];
UINavigationController *nav4=[[UINavigationController alloc]initWithRootViewController:help];
UINavigationController *nav5 =[[UINavigationController alloc]initWithRootViewController:news];


tab=[[UITabBarController alloc]init];
tab.viewControllers=[[NSArray alloc]initWithObjects:nav1,nav2,nav5,nav3,nav4 ,nil];
UITabBarItem *tabItem = [[[tab tabBar] items] objectAtIndex:0];
[tabItem setTitle:@"Menu"];
tabItem.image=[UIImage imageNamed:@"Menu.png"];
UITabBarItem *tabItem1 = [[[tab tabBar] items] objectAtIndex:1];
[tabItem1 setTitle:@"Emergency"];
tabItem1.image=[UIImage imageNamed:@"Emergency.png"];
UITabBarItem *tabItem2 = [[[tab tabBar] items] objectAtIndex:2];
[tabItem2 setTitle:@"News"];
tabItem2.image=[UIImage imageNamed:@"News.png"];
UITabBarItem *tabItem3 = [[[tab tabBar] items] objectAtIndex:3];
[tabItem3 setTitle:@"Calender"];
tabItem3.image=[UIImage imageNamed:@"Calender.png"];
UITabBarItem *tabItem4 = [[[tab tabBar] items] objectAtIndex:4];
[tabItem4 setTitle:@"Help"];
tabItem4.image=[UIImage imageNamed:@"Help.png"];

if ([Userid isEqualToString:@"null"]) {
        Hard =[[HardSoftScreen alloc] initWithNibName:@"HardSoftScreen" bundle:nil];
        nav=[[UINavigationController alloc]initWithRootViewController:Hard];

}
else
{
    ViewController1 *news=[[ViewController1 alloc]initWithNibName:@"ViewController1" bundle:nil];
    nav=[[UINavigationController alloc]initWithRootViewController:news];
    //nav=[[UINavigationController alloc]initWithRootViewController:tab];
}

当我使用nav=[[UINavigationController alloc]initWithRootViewController:news];

我得到了这样的输出

当我使用nav=[[UINavigationController alloc]initWithRootViewController:tab];

我需要像这样同时调用 UITabBar 控制器和 UINavigationBar 控制器

我也用过这样的代码

ViewConroller1 *login = [[ViewConroller1 alloc] initWithNibName:@"ViewConroller1" bundle:nil];
    AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
    [appDelegate.window setRootViewController:appDelegate.tab];
    [self.navigationController pushViewController:login animated:YES];

但是什么也没发生。

那么如何做到这一点。提前致谢!

【问题讨论】:

    标签: ios objective-c uiviewcontroller uinavigationcontroller uitabbarcontroller


    【解决方案1】:

    在 else 条件下将 tabBarController 设置为窗口的根,以获取嵌入其中的 tabbarcontroller 和导航控制器

            if ([Userid isEqualToString:@"null"]) {
                 Hard =[[HardSoftScreen alloc] initWithNibName:@"HardSoftScreen" bundle:nil];
                 nav = [[UINavigationController alloc]initWithRootViewController:Hard];
                 [self.window setRootViewController:nav];
            }
            else
            {
                [self.window setRootViewController:tab];
                [self.window makeKeyAndVisible];
            }
    

    【讨论】:

    • 它显示黑色窗口。我还需要在 else 块中调用 viewcontroller1
    • viewcontroller1 是选项卡栏上的第一个选项卡,因此如果您选择选项卡作为根,它将显示。请添加您的 didFinishLaunchingWithOptions 方法的完整代码以获得清晰的想法
    • 不,我正在检查如果 userid =null 则 HardSoftScreen viewController 将显示,否则 viewcontroller1 将显示。
    • HardSoftScreen 是没有导航和标签栏的简单视图,viewcontroller1 包含两者。
    • 你在 else 条件后使用 self.window.rootViewController = nav 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多