UITabbarController中的Obj-C,UINavigationControllers,用简单的术语解释一下?

时间:2011-12-30 17:24:00

标签: objective-c ios cocoa-touch uinavigationcontroller uitabbarcontroller

我正在玩一些示例代码,试图一劳永逸地弄清楚如何使导航控制器和标签控制器协同工作。作为没有内存泄漏的奖励。

遇到如下所示的问题......

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
        (NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] 
        bounds]] autorelease];

    // Override point for customization after application launch.
    UIViewController *viewController1 = [[[FirstViewController alloc] 
            initWithNibName:@"FirstViewController" bundle:nil] autorelease];
    UIViewController *viewController2 = [[[SecondViewController alloc] 
            initWithNibName:@"SecondViewController" bundle:nil] autorelease];

    self.tabBarController = [[[UITabBarController alloc] init] autorelease];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:
         viewController1, viewController2, nil];
    self.window.rootViewController = self.tabBarController;
    self.navigationController = [[UINavigationController alloc] 
         initWithRootViewController:self.tabBarController]; <<<<

    [self.window makeKeyAndVisible];
    return YES;

在我的主项目中,我有一个4个不同导航控制器的插座,我就像这样打电话。

MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] 
      delegate];
[delegate.balNavController pushViewController:nextController animated:YES];

但这会泄漏并导致问题。

如果不使用界面构建器,有人可以用简单的术语告诉我,我应该如何做到这一点,也许只需几行代码。

1 个答案:

答案 0 :(得分:2)

我强烈建议您选择Big Nerd Ranch Guide to iPhone Programming的副本。它有很好的展示和例子,让你精通基础知识。现在谈谈你的问题......

通过UITabBarController NSArray甚至UIViewController来设置UINavigationController。 Tab Bar不介意哪个。

通过为UINavigationController提供UIViewController来设置- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // UIViewController with UINavigationBar UIViewController *firstViewController = [[UIViewController alloc] init]; UINavigationController *firstNavController = [[UINavigationController alloc] initWithRootViewController:firstViewController]; // Custom subclass of UIViewController with UINavigationBar CustomViewController *secondViewController = [[CustomViewController alloc] init]; UINavigationController *secondNavController = [[UINavigationController alloc] initWithRootViewController:secondViewController]; // UIViewController without UINavigationBar UIViewController *thirdViewController = [[UIViewController alloc] init]; // Set the NSArray of ViewControllers NSArray *viewControllers = [NSArray arrayWithObjects:firstNavController, secondNavController, thirdViewController, nil]; // This array retains the controllers, so go ahead and release them now [firstNavController release]; [secondNavController release]; [thirdViewController release]; // Set up the UITabBarController - It now holds everything tabBarController = [[UITabBarController alloc] init]; [tabBarController setViewControllers:viewControllers]; // Add the Tab Bar Controller to the window. [window addSubview:[tabBarController view]]; [window makeKeyAndVisible]; return YES; }

请记住,标签栏不是视图控制器,因此它不能是导航控制器的根目录!

将这些结合起来只是一个正确的顺序问题。这是一个有希望说明这一点的通用示例。

{{1}}