【问题标题】:Tabbar delegate method is not getting called after changing storyboard更改情节提要后未调用 Tabbar 委托方法
【发布时间】:2018-02-27 07:43:49
【问题描述】:

我在登录情节提要中有 loginsignup 视图控制器,而不是在主情节提要中。

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

中的选项卡委托方法

但是,如果用户已经成功注册或登录,则它会调用以下标签栏委托方法。

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。

#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];

【问题讨论】:

    标签: ios objective-c uitabbarcontroller


    【解决方案1】:

    请在下面找到代码。

    - (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的按钮点击方法,

    - (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上,将ObjectObject Library拖到First Responder下面标签栏控制器场景,将对象类设置为AppDelegate,然后右键单击标签栏控制器并将代理设置为该对象类,如下图所示。

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

    【讨论】:

      猜你喜欢
      • 2016-05-03
      • 2016-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多