我在我的iPhone应用程序中添加了UITabBarController。我在我的TabsAppDelegate.h中提到过这样的
#import <UIKit/UIKit.h>
@interface TabsAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
UIWindow *window;
UITabBarController *tabBarController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@end
和我的TabsAppDelegate.m didFinishLaunchingWithOptions方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions {
// Add the tab bar controller's view to the window and display.
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
return YES;
}
现在在MainWindow.xib中我添加了Tab Bar控制器并与tabBarController建立连接。 后来我创建了FirstViewController和SeconViewController这两个ViewControllers。 现在在第一个选项卡上我添加了我的FirstViewController.xib,在第二个选项卡上我使用builder界面添加了SecondViewController.xib文件。
但是当我运行该项目时,它会显示黑屏。 需要你的帮助。 提前谢谢。
答案 0 :(得分:2)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIViewController *viewController1 = [[[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil] autorelease];
UINavigationController *navviewController1=[[UINavigationController alloc]initWithRootViewController:viewController1];
navviewController1.title = @"FirstTitle";
// navviewController1.navigationBarHidden=YES;
UIViewController *viewController2 = [[[yourviewController2 alloc] initWithNibName:@"yourviewController2" bundle:nil] autorelease];
UINavigationController *navviewController2=[[UINavigationController alloc]initWithRootViewController:viewController2];
// navviewController2.navigationBarHidden=YES;
navviewController2.title = @"SecondTitle";
UIViewController *viewController3 = [[[yourviewController3 alloc] initWithNibName:@"yourviewController2" bundle:nil] autorelease];
UINavigationController *navviewController3=[[UINavigationController alloc]initWithRootViewController:viewController3];
// navviewController3.navigationBarHidden=YES;
navviewController3.title = @"ThirdTitle";
//..... and so on depend on your requirement
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navviewController1, navviewController2 , navviewController3 ,nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
}
试试这个你忘了添加rootViewController
答案 1 :(得分:0)
我创建了一个带有“SignleView”模板的项目,问题我将其编程转换为tabBarViewCongtroller。请注意。仅举例来说,我创建了viewCongtroller.xib实例,四次在四个选项卡上查看,可以添加单独的viewControllers。
这是我的代码,并在iPhone Simulater 5.1上进行了测试。
@implementation AppDelegate
@synthesize window = _window;
@synthesize viewController = _viewController;
@synthesize tb;
- (void)dealloc
{
[_window release];
[_viewController release];
[tb release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSMutableArray *arr=[[NSMutableArray alloc]init];
ViewController *vc1=[[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
vc1.title=@"View1";
vc1.tabBarItem=[[[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:1]autorelease];
UINavigationController *nv1=[[UINavigationController alloc]initWithRootViewController:vc1];
[arr addObject:nv1];
[vc1 release];
[nv1 release];
ViewController *vc2=[[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
vc2.title=@"View2";
UINavigationController *nv2=[[UINavigationController alloc]initWithRootViewController:vc2];
vc2.tabBarItem=[[[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:2]autorelease];
[arr addObject:nv2];
[vc2 release];
[nv2 release];
ViewController *vc3=[[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
vc3.title=@"View3";
UINavigationController *nv3=[[UINavigationController alloc]initWithRootViewController:vc3];
vc3.tabBarItem=[[[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFeatured tag:3]autorelease];
[arr addObject:nv3];
[vc3 release];
[nv3 release];
ViewController *vc4=[[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
vc4.title=@"View4";
vc4.tabBarItem=[[[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemHistory tag:4]autorelease];
UINavigationController *nv4=[[UINavigationController alloc]initWithRootViewController:vc4];
[arr addObject:nv4];
[vc4 release];
[nv4 release];
self.tb=[[[UITabBarController alloc]init]autorelease];
tb.delegate=self;
tb.viewControllers=arr;
[arr release];
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
// self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
self.window.rootViewController = self.tb;
[self.window makeKeyAndVisible];
return YES;
}