【问题标题】:I want to implement TabBarController and add Navigation Controller View but in vain我想实现 TabBarController 并添加 Navigation Controller View 但徒劳无功
【发布时间】:2011-11-29 11:23:10
【问题描述】:

我知道如何通过 Interface Builder 添加一个带有两个 TabBar 按钮的 TabBar。我将每个按钮链接到一个导航控制器。

现在我想学习如何以编程方式完成所有工作。现在我可以在模拟器中看到一个 TabBar,但上面没有按钮。有人可以帮我解决这个问题。谢谢!

这是 TabBarAppDelegate.h。

#import <UIKit/UIKit.h>
@interface TabBarAppDelegate : NSObject <UIApplicationDelegate> 
{
    UIWindow *window;
    UITabBarController *tabBarController;
    IBOutlet UINavigationController *navigationController1;
    IBOutlet UINavigationController *navigationController2;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) UITabBarController *tabBarController;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController1;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController2;
@end

这是 TabBarAppDelegate.m。

#import "TabBarAppDelegate.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
@implementation TabBarAppDelegate
@synthesize window=window;
@synthesize tabBarController;
@synthesize navigationController1;
@synthesize navigationController2;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    tabBarController = [[UITabBarController alloc]init];   
    FirstViewController *firstViewController = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
    firstViewController.title = @"First";
    [self.navigationController1 pushViewController:firstViewController animated:NO];

    SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    secondViewController.title = @"NavCal";
    [self.navigationController2 pushViewController:secondViewController animated:NO];

    tabBarController.viewControllers = [NSArray arrayWithObjects:navigationController1, navigationController2, nil];

    [window addSubview:tabBarController.view];
    [self.window makeKeyAndVisible];
    [firstViewController release];
    [secondViewController release];
    return YES;
}

- (void)dealloc
{
    [tabBarController release];
    [window release];
    [super dealloc];
}

【问题讨论】:

    标签: iphone objective-c uinavigationcontroller uitabbarcontroller tabbar


    【解决方案1】:

    您似乎没有初始化导航视图控制器。看看这是否可行:

    TabBarAppDelegate.h

    • 移除属性 navigationController1 和 navigationController2

    TabBarAppDelegate.m

    • 替换

      [self.navigationController1 pushViewController:firstViewController animated:NO];
      

    UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:firstViewController];
    
    • 替换

      [self.navigationController2 pushViewController:secondViewController animated:NO];
      

    UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:secondViewController];
    
    • 将 navigationController1 和 navigationController2 添加到 tabBarController 后释放它们

    【讨论】:

    • 感谢您的回复。但是这样一来,navigationController1 和 navigationController2 将在 AppDelegate.m 之外过期。如果我仍然想将另一个视图推送到 FirstViewController 怎么办?
    • 要将另一个视图推送到 FirstViewController,在 FirstViewController 中您将使用 [self.navigationController pushViewController:animated:]
    • 我现在完全明白了。非常感谢。
    猜你喜欢
    • 2017-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多