OStack程序员社区-中国程序员成长平台

标题: ios - 更改 Storyboard 后未调用 Tabbar 委托(delegate)方法 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 14:37
标题: ios - 更改 Storyboard 后未调用 Tabbar 委托(delegate)方法

我在登录 Storyboard 中有 loginsignup View Controller ,而不是在主 Storyboard 中。

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

中的选项卡委托(delegate)方法

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

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)applicationUIApplication *)application didFinishLaunchingWithOptionsNSDictionary *)launchOptions {
    CustomTabBarViewController *tabBarController = (CustomTabBarViewController *)self.window.rootViewController;
    tabBarController.delegate = self;
    return YES;
}

-(void) tabBarControllerUITabBarController *)tabBarController didSelectViewControllerUIViewController *)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];



Best Answer-推荐答案


请在下面找到代码。

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

-(void) tabBarControllerUITabBarController *)tabBarController didSelectViewControllerUIViewController *)viewController{
    NSLog(@"Selected Index - %lu",(unsigned long)tabBarController.selectedIndex);
}

关于ViewController的按钮点击方法,

- (IBAction)btnLoginTappedid)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下面标签栏 Controller 场景,将对象类设置为AppDelegate,然后右键单击标签栏 Controller 并将代理设置为该对象类,如下图所示。

enter image description here

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

关于ios - 更改 Storyboard 后未调用 Tabbar 委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48981920/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4