我在主故事板中没有的登录故事板中有登录和注册 viewcontrollers。
一旦注册或登录成功,我就会将Login storyboard更改为Main。以下代码有效但当我选择任何选项卡时,它不会在 AppDelegate
中调用tab delegate方法但是,如果用户已成功注册或登录,则会调用以下tabbar委托方法。
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];
答案 0 :(得分:3)
请查看以下代码。
- (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 上,从对象库拖动对象,并将其放在第一响应者下面标签栏控制器场景,将对象类设置为AppDelegate ,然后右键单击标签栏控制器并将委托设置为该对象类,如下图所示。
让我知道它是否有效,我已准备好帮助解决方案。