Xcode 5中的Root View Controller错误

时间:2014-05-21 12:47:03

标签: ios objective-c xcode

我为我的应用程序编写了这段代码但是当我启动应用程序时,我收到了这条消息:

Application windows are expected to have a root view controller at the end of application launch

以下是代码:

NSMutableArray *controllers = [[NSMutableArray alloc] initWithCapacity:2];
_tabBarController = [[UITabBarController alloc]init];
ListViewController *listViewController = [[ListViewController alloc]initWithNibName:nil bundle:nil];
listViewController.title = @"Lista";
listViewController.tabBarItem.image = [UIImage imageNamed:@"list.jpg"];
InsertViewController *insertViewController = [[InsertViewController alloc]initWithNibName:nil bundle:nil];
insertViewController.title = @"Inserimento";
insertViewController.tabBarItem.image = [UIImage imageNamed:@"plus.jpg"];
[controllers addObject:insertViewController];
[controllers addObject:listViewController];
_tabBarController.viewControllers = controllers;

我试着添加这个:

UIViewController *viewController = [[UIViewController alloc] init];
self.window.rootViewController = viewController;

但是当我启动时,应用程序全部为白色,并且不会出现错误。

这是我的appdelegate.m我有一个导入头文件并写入appdelegate.h这个:

@interface AppDelegate : UIResponder <UIApplicationDelegate>{
UITabBarController *_tabBarController
}

2 个答案:

答案 0 :(得分:0)

你的AppDelegate.m

中的

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

                NSMutableArray *controllers = [[NSMutableArray alloc] initWithCapacity:2];
                _tabBarController = [[UITabBarController alloc]init];
                ListViewController *listViewController = [[ListViewController alloc]initWithNibName:nil bundle:nil];
                listViewController.title = @"Lista";
                listViewController.tabBarItem.image = [UIImage imageNamed:@"list.jpg"];
                InsertViewController *insertViewController = [[InsertViewController alloc]initWithNibName:nil bundle:nil];
                insertViewController.title = @"Inserimento";
                insertViewController.tabBarItem.image = [UIImage imageNamed:@"plus.jpg"];
                [controllers addObject:insertViewController];
                [controllers addObject:listViewController];
                _tabBarController.viewControllers = controllers;
                self.window.rootViewController = _tabBarController;

                [self.window makeKeyAndVisible];
            }

答案 1 :(得分:0)

您需要将tabBarController设置为rootViewController的{​​{1}}。要执行此操作,请在UIWindow AppDelegate.m方法内,在didFinishLaunchingWithOptions行之前添加[self.window makeKeyAndVisible]。希望这会有所帮助。