[英]Tabbar delegate method is not getting called after changing storyboard
我在登录情节提要中没有登录和注册视图控制器,而在主情节提要中没有。
一旦注册成功或登录成功,然后将Login Storyboard更改为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];
请找到下面的代码。
- (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的Button Click方法上,
- (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,从对象库拖动对象 ,并把它下面急救员在标签栏控制器的场景,设置对象类的AppDelegate,然后右键点击标签栏控制器,并设置委托给该对象类如下图所示的图像。
让我知道它是否有效,我准备为解决方案提供帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.