2015-10-19 60 views
-1

我更新版本的Xcode,現在我得到以下錯誤:addSubview到RootViewController的

*application windows are expected to have a root view controller*

我讀錯誤是因爲我沒有RootViewController的分配。

問題是我有多個子視圖,任何是主視圖。 例子:

// Add the view controller's view to the window and display. 
[window addSubview:infoTabViewController.view]; 
[window addSubview:shareTabViewController.view]; 
[window addSubview:tabViewController.view]; 
[window addSubview:mainTabViewController.view]; 

你怎麼能分配這些子視圖到RootViewController的?

謝謝。

回答

0

UITabBarController並使其成爲rootViewController。 並在此TabBarController上添加所有子視圖。

代碼示例

self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; 

UITabBarController *tabBarController = [[UITabBarController alloc]init]; 
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]]; 

FirstViewController *firstViewController = [storyboard instantiateViewControllerWithIdentifier:@"FirstViewController"]; 

SecondViewController *secondViewController = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"]; 

tabBarController.viewControllers = @[firstViewController,secondViewController]; 

[self.window setRootViewController: tabBarController]; 

[self.window makeKeyAndVisible]; 

但不加NavigationController

0

首先你appdelegate.m編寫添加的TabBar控制器後設置根視圖控制器

- (BOOL)application:(UIApplication)application didFinishLaunchingWithOptions:(NSDictionary)launchOptions { 
    // Override point for customization after application launch. 

    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]]; 

    self.logviewController = [[LoginViewController alloc]init]; 

    self.navigationController = [[UINavigationController alloc]initWithRootViewController:self.logviewController]; 
    [self.window setRootViewController: self.navigationController]; 

    [self.window makeKeyAndVisible]; 



    return YES; 
} 

和在代碼中寫代碼確實加載了

- (void)viewDidLoad 

{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

    self.navigationItem.leftBarButtonItem=nil; 
    self.navigationItem.hidesBackButton=YES; 


    self.view.backgroundColor = [UIColor whiteColor]; 
    self.title = @"Welcome To MyConnect"; 


    _friendviewcontroller = [FriendsViewController new]; 
    _homeviewcontroller = [HomePageViewController new]; 
    _notifviewcontroller = [NotificationViewController new]; 
    _messageviewcontroller = [MessagesViewController new]; 

    self.tabbarcontroller = [[UITabBarController alloc]init]; 

    self.viewControllers = [NSArray arrayWithObjects:_homeviewcontroller,_messageviewcontroller,_friendviewcontroller,_notifviewcontroller, nil]; 

    //UITabBar *tabBar = self.tabBarController.tabBar; 



    UITabBarItem *item0 = [self.tabBar.items objectAtIndex:0]; 
    UITabBarItem *item1 = [self.tabBar.items objectAtIndex:1]; 
    UITabBarItem *item2 = [self.tabBar.items objectAtIndex:2]; 
    UITabBarItem *item3 = [self.tabBar.items objectAtIndex:3]; 


    [item0 setFinishedSelectedImage:[UIImage imageNamed:@"blue-home-icon.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"home_2.png"]]; 

    [item1 setFinishedSelectedImage:[UIImage imageNamed:@"email-icon-env.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"message_2.png"]]; 

    [item2 setFinishedSelectedImage:[UIImage imageNamed:@"friendreq_icon.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"friendreq_2.jpg"]]; 

    [item3 setFinishedSelectedImage:[UIImage imageNamed:@"notification_icon.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"bell-512.png"]]; 




    UIImage *img1=[UIImage imageNamed:@"menu-2-128.png"]; 
    CGRect frameimg1 = CGRectMake(340, 0, 35,35); 


    UIButton *signOut=[[UIButton alloc]initWithFrame:frameimg1]; 
    [signOut setBackgroundImage:img1 forState:UIControlStateNormal]; 
    [signOut addTarget:self action:@selector(collectionmenutwo:)forControlEvents:UIControlEventTouchUpInside]; 
    [signOut setShowsTouchWhenHighlighted:YES]; 

    UIBarButtonItem *barButton=[[UIBarButtonItem alloc]initWithCustomView:signOut]; 
    self.navigationItem.rightBarButtonItem=barButton; 




    item0.title = @"Home"; 
    item1.title = @"Messages"; 
    item2.title = @"Friends Requests"; 
    item3.title = @"Notification"; 
}