更改故事板后未调用 Tabbar 委托方法 [英] Tabbar delegate method is not getting called after changing storyboard

查看:25
本文介绍了更改故事板后未调用 Tabbar 委托方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在登录故事板而不是主故事板中有登录注册视图控制器.

I have login and signup viewcontrollers in Login storyboard not in the Main storyboard.

注册或登录成功后,我将登录故事板更改为 Main.以下代码有效,但是当我选择任何选项卡时,它不会调用 AppDelegate

Once signup or login is successful, then I am changing Login storyboard to Main. The following code works but when I select any tab, it does not call tab delegate method in the AppDelegate

但是,如果用户已经成功注册或登录,那么它会调用下面的标签栏委托方法.

However, if user is already succesfully signup or logged in, then it calls the following tabbar delegate method.

LoginViewController.m

if(isLoginSuccess)
{
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    CustomTabBarViewController *tbc = [mainStoryboard instantiateViewControllerWithIdentifier:@"tabbar"];
    tbc.selectedIndex = 2;
    [self presentViewController:tbc animated:YES completion:nil];
}

AppDeletage.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    CustomTabBarViewController *tabBarController = (CustomTabBarViewController *)self.window.rootViewController;
    tabBarController.delegate = self;
    return YES;
}

-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    if (tabBarController.selectedIndex == 2) {
        if(![self isRegistered])
        {
            UIStoryboard *loginStoryboard = [UIStoryboard storyboardWithName:@"LoginStoryboard" bundle:nil];
            UIViewController *vc = [loginStoryboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
            [ROOTVIEW presentViewController:vc animated:YES completion:nil];
        }
        else
        {
            tabBarController.selectedIndex = 2;
        }
    }
}

更新:

AppDelegate 没有被调用,但我想知道为什么以下代码在用户注销后没有在 AppDelegate 中打开 Loginstoryboard.

AppDelegate is not getting called but I wonder why the following code does not open Loginstoryboard in the AppDelegate after user is logout.

#define ROOTVIEW [[[UIApplication sharedApplication] keyWindow] rootViewController]


UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"LoginStoryboard" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
[ROOTVIEW presentViewController:vc animated:YES completion:nil];

推荐答案

请在下面找到代码.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UITabBarController *tabController = [main instantiateViewControllerWithIdentifier:@"TabbarController"];
    tabController.delegate = self;
    return YES;
}

-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    NSLog(@"Selected Index - %lu",(unsigned long)tabBarController.selectedIndex);
}

关于 ViewController 的按钮点击方法,

On ViewController's Button Click Method,

- (IBAction)btnLoginTapped:(id)sender {
    UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UITabBarController *tabController = [main instantiateViewControllerWithIdentifier:@"TabbarController"];
    tabController.selectedIndex = 1;
    [self presentViewController:tabController animated:YES completion:nil];
}

然后在Main.storyboard上,将对象对象库拖放到First Responder下面标签栏控制器场景,将对象类设置为 AppDelegate,然后右键单击标签栏控制器并将委托设置为该对象类,如下图所示.

then on Main.storyboard, Drag Object from the Object Library and put it below First Responder in Tab bar Controller Scene, Set the Object class to AppDelegate, then right click on Tab Bar Controller and set delegate to that Object Class as shown in below image.

让我知道它是否有效,我已准备好帮助解决.

Let me know it is working or not, I'm ready to help with the solution.

这篇关于更改故事板后未调用 Tabbar 委托方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆