[英]addSubview to rootViewController
我更新了我的 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?
谢谢。
取一个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
首先你写在 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;
}
之后添加标签栏控制器并在视图中编写代码确实加载了
- (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";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.