gpt4 book ai didi

ios - 更改 Storyboard后未调用 Tabbar 委托(delegate)方法

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:53:24 25 4
gpt4 key购买 nike

我在登录 Storyboard 中有登录注册 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)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];

最佳答案

请找到下面的代码。

- (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下面Tab bar Controller Scene,将 Object class 设置为 AppDelegate,然后右键单击 Tab Bar Controller 并set delegate to that Object Class,如下图所示。

enter image description here

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com