我想制作应用程序,其中第一个是登录屏幕,我diidnot想要tabbar在其中因为我想从第二个屏幕的tabbar。但是当我在appdelegate中写tabbar代码时它也进入登录屏幕。那么如何编码呢?
答案 0 :(得分:2)
将它放在您想要添加TabBar
的控制器屏幕中UIViewController *vc1 = [[UIViewController alloc] init];
vc1.title = @"FIRST";
vc1.view.backgroundColor = [UIColor blueColor];
UIViewController *vc2 = [[UIViewController alloc] init];
vc2.title = @"SECOND";
vc2.view.backgroundColor = [UIColor redColor];
UITabBarController *tabBar = [[UITabBarController alloc] init];
tabBar.viewControllers = @[vc1,vc2];
tabBar.selectedIndex = 1;
tabBar.view.frame = CGRectMake(50, 50, 220, 320);
[tabBar willMoveToParentViewController:self];
[self.view addSubview:tabBar.view];
[self addChildViewController:tabBar];
[tabBar didMoveToParentViewController:self];
答案 1 :(得分:1)
如果您使用的是故事板,那么我建议使用以下方法:
拖动UIViewController
并将其嵌入UINavigationController
。这将是您的LoginViewController包含用户名,密码和登录按钮。
现在在故事板上添加UITabbarController
并将其Storyboard Id
作为'mainTab' - 用于保存标签项。
现在只需从LoginViewController上点击登录按钮,然后在您想要登录到标签栏控制器时添加以下代码行。
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UITabBarController *obj=[storyboard instantiateViewControllerWithIdentifier:@"MainTab"];
self.navigationController.navigationBarHidden=YES;
[self.navigationController pushViewController:obj animated:YES];
是的,假设您的故事板名称是MainStoryboard。 希望它有所帮助。
答案 2 :(得分:1)
从appDelegate显示您的登录屏幕。然后从登录viewController中显示第二个带tabBar的viewController。使用以下代码。
//create a UITabBarController object
UITabBarController *tabBarController=[[UITabBarController alloc]init];
//FirstViewController and SecondViewController are the view controllers you want on your UITabBarController (Number of view controllers can be according to your need)
FirstViewController *firstViewController=[[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];
SecondViewController *secondViewController=[[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
//adding view controllers to your tabBarController bundling them in an array
tabBarController.viewControllers=[NSArray arrayWithObjects:firstViewController,secondViewController, nil];
//navigating to the UITabBarController that you created
[self.navigationController pushViewController:tabBarController animated:YES];