问题
I have login and signup viewcontrollers in Login storyboard not in the Main storyboard.
Once signup or login is successful, then I am changing Login storyboard to Main. The following code works but when I select any tab, it does not call tab delegate method in the AppDelegate
However, if user is already succesfully signup or logged in, then it calls the following tabbar delegate method.
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;
}
}
}
Update :
AppDelegate is not getting called but I wonder why the following code does not open Loginstoryboard in the AppDelegate after user is logout.
#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];
回答1:
Please find below code.
- (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);
}
On ViewController's Button Click Method,
- (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];
}
then on Main.storyboard, Drag Object from the Object Library and put it below First Responder in Tab bar Controller Scene, Set the Object class to AppDelegate, then right click on Tab Bar Controller and set delegate to that Object Class as shown in below image.
Let me know it is working or not, I'm ready to help with the solution.
来源:https://stackoverflow.com/questions/48981920/tabbar-delegate-method-is-not-getting-called-after-changing-storyboard