3

我在我的 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 建立连接。后来我创建了两个 ViewController 的 FirstViewController 和 SeconViewController。现在在第一个选项卡上我添加了我的 FirstViewController.xib,在第二个选项卡上我使用构建器界面添加了 SecondViewController.xib 文件。

但是当我运行该项目时,它显示黑屏。需要你的帮助。提前致谢。

4

2 回答 2

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

于 2012-10-18T09:21:23.340 回答
0

I have created a project with "SignleView" Template for you question I converted it programmatically to tabBarViewCongtroller. Please note that. only for example I created viewCongtroller.xib instance four time to view on four tab you can add separate viewControllers.

Here is my code and it tested on 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;
}
于 2012-10-18T10:19:22.133 回答