IOS - How to program the navigation between Viewcontroller direct in viewDidLoad?

Asked

Viewed 185 times

1

I want to navigate between the Viewcontroller of an app by clicking a button and switching from one to the other for example. I wanted to run this directly in the viewDidLoad of Viewcontroller1, if the user has already registered, I send it to secondView.

It would be something like that:

Viewcontroller1

-(void)viewDidLoad{
    if(_cadastrado isEqualToString:@"1"){
        UIViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"segundaView"];
        [self presentViewController:viewController animated:YES completion:nil];
    }
}

The method when running out of viewDidLoad works perfectly, however, when I try to run it inside viewDidLoad I cannot.

  • 1

    Try to put inside the viewDidAppear, because you can only make screen calls after the current view is already being displayed

  • I’m trying here, so I’ll let you know if I made it.

  • It worked! Thank you Jeferson.

2 answers

2

Hello, in this case it will be necessary to carry out this process in Appdelegate

Example This:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    // EM - Implementação da verificação do cadastro da função do Device
    NSIndexPath * indexPath;

    [PlistManager deletePlist:@"ServerControllerPWD" location:@"doc"];

    Password * passConference = [[PWDataController sharedInstance].PWData objectAtIndex:indexPath.row];

    if ([passConference.functionVC isEqualToString:@"Servidor"]) {

        ServerViewController *appStartViewController = [[ServerViewController alloc] init];
        UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:appStartViewController];
        [navController setModalPresentationStyle:UIModalPresentationFullScreen];

        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

        UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"Server"];
        self.window.rootViewController = vc;

        [self.window makeKeyAndVisible];

    } else {

     nil ;

return YES;

}

This example is taking the data of an object that has the data stored in a Plist.

Make this comparison if there is data or not, if there is data it opens the registration screen, if there is data it opens the related view.

In your case this "_register" refers to what ?

  • Hello Elias, in the case **_Cadastro*, is a Nsstring and its content is a result that warns me if the user was registered or not, my question was the same screen change. But I managed to add my code that I quoted in the question within the 'viewDidAppear' method quoted by @Jefferson Assis

  • 1

    That, in this way is also functional, how nice to have succeeded! hugs!

1


With the tip from @Jeffersonassis, I managed to include the code mentioned in the question within the method viewDidAppear.

was thus:

-(void) viewDidAppear:(BOOL)animated{
    if(_cadastrado isEqualToString:@"1"){
        UIViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"segundaView"];
        [self presentViewController:viewController animated:YES completion:nil];
    }
}

Thus, it executes soon after it has run the whole viewDidLoad.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.