我想实现TabBarController并添加导航控制器视图但是徒劳无功

时间:2011-10-02 16:44:06

标签: iphone objective-c uinavigationcontroller uitabbarcontroller tabbar

我知道如何通过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];
}

1 个答案:

答案 0 :(得分: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
  • 后发布