关闭当前UIViewcontroller并呈现一个新的UiViewController。

5
我想要关闭当前的UIViewController并展示一个新的UIViewController。我使用了以下代码:
 let newViewController: ViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController") as! ViewController
 self.presentViewController(newViewController, animated: false, completion: {
         self.dismissViewControllerAnimated(false, completion: nil)
    })

它出现了以下错误:
2016年06月04日11:40:59.864 myApp [851:117649]尝试在转换期间解除演示控制器。 (< _UIFullscreenPresentationController:0x1703e6900>) 2016年06月04日11:40:59.878 ePassBook [851:117649]未设置CurrentTransition的transitionView,演示控制器在演示期间被解除了? (< _UIFullscreenPresentationController:0x1703e6900>)

点击取消操作以关闭UIViewController,然后自动呈现另一个ViewController? - Iyyappan Ravi
3个回答

1

使用此代码:

Objective C 代码:

[self.navigationController presentViewController:newViewController animated:NO completion:^{
    dispatch_after(0, dispatch_get_main_queue(), ^{
        [self.navigationController dismissViewControllerAnimated:NO completion:nil];
    });
}];

Swift 代码:

self.navigationController?.presentViewController(newViewController, animated: false, completion: { () -> Void in
            dispatch_after(0, dispatch_get_main_queue(), { () -> Void in
                self.navigationController?.dismissViewControllerAnimated(false, completion: nil)

            })
        })

希望有所帮助。

你需要先关闭当前显示的VC,然后才能呈现另一个。 - Ketan Parmar

1
尝试这段代码,我认为它很有帮助。
目标-C。
[self.navigationController presentViewController:newViewController animated:NO completion:^{
    dispatch_async(dispatch_get_main_queue(), ^(void){
    //Run UI Updates

        [self.navigationController dismissViewControllerAnimated:NO completion:nil];
    });
}];

SWIFT

self.navigationController?.presentViewController(newViewController, animated: false, completion: { () -> Void in
           dispatch_async(dispatch_get_main_queue()) {
                self.navigationController?.dismissViewControllerAnimated(false, completion: nil)

        }
    })

感谢您的回复@Ravi。我尝试了您的代码,但它没有起作用......我正在实例化的控制器没有嵌入在UINavigationController中。 - Cloy

0

你首先需要dismiss当前显示的viewcontroller,然后再present另一个,因为一次只能呈现两个viewcontroller,所以你的代码应该像这样:

  self.dismissViewControllerAnimated(false) { () -> Void in

        self.presentViewController(newViewController, animated: true, completion: nil)

    }

如果您正在使用导航控制器,则在导航控制器上呈现或解除VC。

根据评论要求更新:

举个例子,

您有三个视图控制器,称为A、B和C,当前呈现的视图控制器是C。像A -> B -> C这样

现在,您想要关闭C并呈现新的D,那么您必须创建B的实例,因为您正在关闭C,所以self什么也没有。它是nil。

因此,您应该创建B的实例,比如说b,并在b上呈现D。

类似于以下内容:

 self.dismissViewControllerAnimated(false) { () -> Void in

        b.presentViewController(d, animated: true, completion: nil)

    }

如果你正在使用导航控制器,那么它应该像这样,
   b.navigationController.presentViewCon.....

那么会发生什么? - Ketan Parmar
它会关闭当前的视图控制器,但不会创建新的视图控制器。 - Cloy
你需要创建一个VC的实例,该实例位于你呈现的VC后面,然后在该VC的实例上呈现你的视图,例如 self.vc.presentView.....,这里的vc是你首先dismiss的VC后面的VC的实例。 - Ketan Parmar
谢谢@Lion.....上面的解决方案有效......但它会返回到之前的视图控制器,然后呈现一个新的视图控制器......有没有正确的方法来做这个或者这似乎是一个hack? - Cloy
1
如果可以的话,尝试在主线程上呈现新的视图控制器! - Ketan Parmar
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接