addSubview到rootViewController

时间:2015-10-19 11:08:27

标签: ios xcode

我更新了我的XCode版本,现在我收到以下错误:

*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?

感谢。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

首先,您在appdelegate.m中写入以设置根视图控制器

- (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;
}

之后添加tabbar控制器并在视图中写入代码加载

- (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";
}