简体   繁体   English

ios:如何使用StoryBoards从ViewController打开Tab Bar控制器

[英]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 如下面的代码MainTabBarUITabBarController 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.

相关问题 使用情节提要板ioS5时从视图控制器移动到选项卡栏控制器 - Moving from a view controller to a tab bar controller when using storyboards ioS5 如何使用故事板与标签栏控制器作为初始控制器共享UIManagedDocument? - How to share a UIManagedDocument using Storyboards with a Tab Bar Controller as initial controller? 与选项卡栏控制器一起使用多个情节提要 - Using Multiple Storyboards with Tab Bar Controller 如何在标签栏iOS中将数据从ViewController传递到ViewController? - How to pass data from ViewController to a ViewController in a tab bar iOS? 移动到另一个视图时标签栏控制器消失(iOS SDK,使用故事板) - Tab Bar controller disappearing when moving to another view (iOS SDK, Using storyboards) 如何从嵌入在标签栏 Controller 中的 UIButton 推送 ViewController - How to push a ViewController from a UIButton that is embedded in Tab Bar Controller StoryBoards从View Controller导航到Tab视图控制器iOS 5 - StoryBoards navigating from View Controller to Tab view controller iOS 5 将数据从标签栏 controller 传输到 ViewController - Transfer data from tab bar controller to ViewController 在一个标签栏控制器中从一个ViewController切换到另一个ViewController? - Segue from one ViewController to another ViewController inside a Tab Bar Controller? 如何从选项卡栏下面的侧菜单(SWRevealviewcontroller)中打开视图控制器,以使选项卡栏不应该隐藏在底部? - how to open the viewcontroller from the side menu(SWRevealviewcontroller) under the tab bar so that the tab bar should not be hidden at the bottom?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM