使用带有tabbar控制器的navigationcontoller

时间:2012-04-26 15:14:54

标签: ios uinavigationcontroller uitabbarcontroller

我正在尝试制作它,所以我的屏幕底部有一个标签栏,它始终存在。我也想要它,所以如果我“点击”其中一个选项卡中的某个菜单,它会为您提供返回选项,从而导航控制器和标签栏控制器。

我仍然不太了解iOS,所以我找到的答案令我感到困惑。

这个答案:Having a UITabBar AND a UINavigationController in an app?

那么我该如何实现呢?我猜我在App Delegate中改变了这个方法,

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    UIViewController *viewController1, *viewController2;
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        viewController1 = [[CFSDFirstViewController alloc] initWithNibName:@"CFSDFirstViewController_iPhone" bundle:nil];
        viewController2 = [[CFSDSecondViewController alloc] initWithNibName:@"CFSDSecondViewController_iPhone" bundle:nil];
    } else {
        viewController1 = [[CFSDFirstViewController alloc] initWithNibName:@"CFSDFirstViewController_iPad" bundle:nil];
        viewController2 = [[CFSDSecondViewController alloc] initWithNibName:@"CFSDSecondViewController_iPad" bundle:nil];
    }
    self.tabBarController = [[UITabBarController alloc] init];
    [self.tabBarController setDelegate:self];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

感谢您的协助!

1 个答案:

答案 0 :(得分:2)

首先,您不需要手动设置笔尖名称。您可以将它们命名为CFSDFirstViewController~iphone.xibCFSDFirstViewController~ipad.xib。然后你可以拨打[[CFSDFirstViewController alloc] init]并让iOS为你完成剩下的工作。有关信息,请参阅iOS Supports Device-Specific Resources

关于您的问题,您只能在UINavigationController内插入UITabBarController。要将viewController1包裹在UINavigationController中,如下所示:

CFSDFirstViewController viewController1 = [[CFSDFirstViewController alloc] init];
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:viewController1];

然后使用navController代替viewController1,如下所示

self.tabBarController.viewControllers = [NSArray arrayWithObjects:navController, nil];

检查代码,因为我是手写的。如果您使用非ARC项目,请注意内存。

希望它有所帮助。