Obj-C,UITabbarController 中的 UINavigationController,简单解释一下?

发布于 2024-12-23 15:25:52 字数 1490 浏览 2 评论 0原文

我正在使用一些示例代码来尝试一劳永逸地弄清楚如何使导航控制器和选项卡控制器一起工作。作为奖励,没有内存泄漏。

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

- (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];

但这会泄漏并引发问题。

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

I'm playing with some sample code to try and figure out once and for all how to make navigation controller (s) and a tab controller to work together. As a bonus without memory leaks.

Having problems as shown below...

- (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;

In my main project I have an outlet for 4 different navigation controllers and I call each like so.

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

But this leaks and is causing problems.

Without using the interface builder, can someone advise me, in simple terms how I should be doing this, perhaps with a few lines of code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

金橙橙 2024-12-30 15:25:53

我强烈建议您获取一份Big Nerd Ranch iPhone 编程指南。它有出色的阐述和示例,可帮助您精通基础知识。现在讨论您的问题...

通过给它一个 UIViewController 甚至 UINavigationControllerNSArray 来设置 UITabBarController >s。标签栏不介意哪个。

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;
}

I strongly encourage your to pick up a copy of the Big Nerd Ranch Guide to iPhone Programming. It has excellent exposition and examples to get you proficient in the basics. Now on to your issues...

A UITabBarController is set by giving it an NSArray of UIViewControllers or even UINavigationControllers. The Tab Bar doesn't mind which.

A UINavigationController is set by giving it a UIViewController that will be the root.

Keep in mind that the Tab Bar is not a View Controller, so it cannot be the root of a Navigation Controller!

Combining these is simply a matter of correct order. Here's a generic example that hopefully illustrates this.

- (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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文