ios present view controller immediately after dismissing a previous one

别来无恙 提交于 2019-12-24 16:13:23

问题


In my ios app lets say I have three ViewControllers: A, B, and C.

From A I present B and assign A as a delegate. After an action is done on B I want to then dismiss B and present C from A. However, I want to do this without A showing up on the screen at all. This is my code right now, inside class A:

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    B *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"B-identifier"];
    vc.delegate = self;
    [self presentViewController:vc animated:NO completion:^{}];

}

Then this is the delegate function inside A that B calls when the action is performed:

- (void) actionPerformed
{
    [self dismissViewControllerAnimated:YES completion:^{
    C *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"C"];
    [self presentViewController:vc animated:NO completion:nil];
}];

However this causes C to show up for a bit (after calling dismiss) even though I put the presenting code in the completion handler of the dismissal. What can I do to avoid that?


回答1:


If you are using it for login approach then you should try a different approach. I mean if A is your rootViewController(make it as login view controller) which check if user has a session or not. suppose user has a session then make your C viewController as rootViewController using [[[[UIApplication sharedApplication] delegate] window]setRootViewController:] and if he does not has a session show him the same page (A viewController) there is no need of B. Just try it may be it will improve your app performance.




回答2:


You can manage this by timeinterval
like This way

you can first dissmiss the view [self dismiss];

-(void)dismiss
{
    [self dismissViewControllerAnimated:YES completion:nil];
    [self performSelector:@selector(present) withObject:nil afterDelay:2.0];
}
-(void)present
{
    C *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"C"];
    [self presentViewController:vc animated:NO completion:nil];
}


来源:https://stackoverflow.com/questions/26381393/ios-present-view-controller-immediately-after-dismissing-a-previous-one

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