我知道如何通过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];
}
答案 0 :(得分:1)
看起来您没有初始化导航视图控制器。看看这是否有效:
TabBarAppDelegate.h
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];