【问题标题】:Getting Black screen while using tabBar in iPhone application在 iPhone 应用程序中使用 tabBar 时出现黑屏
【发布时间】:2012-10-18 18:19:22
【问题描述】:

我在我的 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 文件。

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

【问题讨论】:

    标签: iphone uitabbarcontroller


    【解决方案1】:
     - (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

    【讨论】:

    • 假设我创建了thirdViewController,那么我还必须将它添加到数组中??我要求确认。
    • Thnks 我会试试的。我能知道为什么 Simple ViewController 后面还有 UINavigationController。是必须的吗?
    • 是的,在这里添加为 rootViewController 您必须使用 UINavigationController 添加...如果您只想使用 SingleViewController 应用程序,则不需要它..
    【解决方案2】:

    我为您创建了一个带有“SignleView”模板的项目,我以编程方式将其转换为 tabBarViewController。请注意。仅例如我创建了 viewController.xib 实例四次以在四个选项卡上查看,您可以添加单独的 viewControllers。

    这是我的代码,它在 iPhone Simulator 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-20
      • 2022-06-26
      • 1970-01-01
      • 1970-01-01
      • 2015-12-09
      • 1970-01-01
      • 2016-01-04
      • 2016-04-14
      相关资源
      最近更新 更多