[英]ios: How open Tab Bar controller from ViewController using StoryBoards
I am doing this and want to open Tab bar Controller from Login Page if its first time and if already login then pass the LoginPage and open Tab Bar Controller after Spalsh 我这样做,想要从登录页面打开标签栏控制器,如果它已经第一次登录然后通过登录页面并在Spalsh后打开标签栏控制器
means if user lands in app fiirst time sequesnce should be like this 1. Splash 2. Login Page 3. On successful Login open Tab Bar Controller having 4 tabs 意味着如果用户在应用程序第一时间登陆,则应该是这样的:1。启动2.登录页面3.成功登录打开标签栏控制器有4个选项卡
if user already login then 1. Splash 2. Tab bar controller 如果用户已登录,则1. Splash 2.标签栏控制器
I am trying to open Tab Bar controller through the following code in ViewDidLoad method 我试图通过ViewDidLoad方法中的以下代码打开Tab Bar控制器
UITabBarController *tbc = [self.storyboard instantiateViewControllerWithIdentifier:@"TripMapViewer"];
tbc.selectedIndex=0;
[self presentViewController:tbc animated:YES completion:nil];
but it gives error 但它给出了错误
2014-02-06 19:55:43.849 ProjNew[1065:907] -[TripMapViewer setSelectedIndex:]: unrecognized selector sent to instance 0x1d5600b0 2014-02-06 19:55:43.849 ProjNew [1065:907] - [TripMapViewer setSelectedIndex:]:无法识别的选择器发送到实例0x1d5600b0
and if I remove tbc.selectedIndex=0; 如果我删除tbc.selectedIndex = 0; it does nothing and stays on Splash screen like this 它什么都不做,就像这样停留在Splash屏幕上
UITabBarController *lbc = [self.storyboard instantiateViewControllerWithIdentifier:@"TripMapViewer"];
[self presentViewController:lbc animated:YES completion:nil];
please suggest that how do I open tab bar from View Controller 请建议我如何从View Controller打开标签栏
It seems that TripMapViewer is the Storyboard ID
of some Tab and not the UITabBarController
, please make it sure and it will work 看来TripMapViewer是某个Tab的Storyboard ID
而不是UITabBarController
,请确保它确实有效
as in the below code MainTabBar is Storyboard ID
of UITabBarController
and it works perfectly 如下面的代码MainTabBar是UITabBarController
Storyboard ID
,它完美地运行
UITabBarController *tbc = [self.storyboard instantiateViewControllerWithIdentifier:@"MainTabBar"];
tbc.selectedIndex=0;
[self presentViewController:tbc animated:YES completion:nil];
You don't need a splash screen controller unless you are animating something. 除非您正在制作动画,否则您不需要启动画面控制器。 This example uses NSUserDefaults to remember if it's first login or not. 此示例使用NSUserDefaults记住它是否首次登录。
In you application delegate put the following: 在您的应用程序委托中提出以下内容:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
if(![[NSUserDefaults standardUserDefaults] dictionaryForKey:@"someKey"]){
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"registerViewController"];
self.window.rootViewController = viewController;
} else {
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"mainViewController"];
self.window.rootViewController = viewController;
}
[self.window makeKeyAndVisible];
return YES;
}
If you decide you must have a viewcontroller for the splash screen then you can put the same code there. 如果你决定必须为启动画面设置一个viewcontroller,那么你可以在那里放置相同的代码。
The fact that the VC being instantiated is not a TabBarController explains the crash. 实例化的VC不是TabBarController的事实解释了崩溃。 The code asks storyboard for a regular vc, casts it as a UITabBarController
, then sends it a message setSelectedIndex
: that it doesn't implement because it's not a tab bar controller. 该代码向storyboard请求常规vc,将其转换为UITabBarController
,然后向其发送消息setSelectedIndex
:它没有实现,因为它不是标签栏控制器。
At least the first step in getting where you want to go is to add an identity in storyboard to the tab bar controller (the for which TripMapViewer is one of the tabs). 至少获得你想去的地方的第一步是将故事板中的标识添加到标签栏控制器(TripMapViewer是其中一个标签)。 Then instantiate that, and the crash should be gone. 然后实例化,崩溃应该消失。
At the design level, please see my question and answer here about how to do login and splash. 在设计层面,请在此处查看我的问题和答案,了解如何进行登录和启动。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.