TabBarController, overwriting shouldSelectViewController to do a segue

为君一笑 提交于 2019-12-23 04:33:48

问题


Hi Im trying to change the tabcontroller flow, so when a user is not loged in just take him to the login view instead the settings one. My controller extends TabBarController and I set the delegate as

self.tabBarController.delegate=self;

My Code is:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {

    if (login) {

        LoginViewController *loginViewController = [[LoginViewController alloc] init];
        UINavigationController *navController = [[UINavigationController alloc]
                                                 initWithRootViewController:loginViewController];


        [tabBarController presentViewController:loginViewController animated:YES completion:nil];
               return NO;
    } else {
        return YES;
    }

I never manage to do the navigation it gives an excetion :

ion 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller <UITabBarController: 0x6a72220>.

I also tried to show the login in as a modal but it only shows a black screen:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {

    if (YES) {
        LoginViewController *loginViewController = [[LoginViewController alloc] init];
        UINavigationController *navController = [[UINavigationController alloc]
                                                 initWithRootViewController:loginViewController];


        [tabBarController presentModalViewController:navController animated:YES];
               return NO;
    } else {
        return YES;
    }
}

Can anybody help Me!!!! please!!!!


回答1:


Well I manage to fix the black modal screen (still cant d a segue that is not modal). The problem was that as I am using storyboard I have to load the view from story board as follows.

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    UIViewController *navController = [storyboard instantiateViewControllerWithIdentifier:@"login"];

    [navController setModalPresentationStyle:UIModalPresentationFullScreen];

    [self presentModalViewController:navController animated:YES];

That made the trick :)



来源:https://stackoverflow.com/questions/9589538/tabbarcontroller-overwriting-shouldselectviewcontroller-to-do-a-segue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!