模态视图控制器-如何显示和关闭

在过去的一周中,我为解决如何显示和消除多个视图控制器问题而烦恼不已。我创建了一个示例项目,并直接从该项目中粘贴代码。我有3个视图控制器及其相应的.xib文件。MainViewController,VC1和VC2。我在主视图控制器上有两个按钮。


- (IBAction)VC1Pressed:(UIButton *)sender

{

    VC1 *vc1 = [[VC1 alloc] initWithNibName:@"VC1" bundle:nil];

    [vc1 setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];

    [self presentViewController:vc1 animated:YES completion:nil];

}

这将打开VC1,没有任何问题。在VC1中,我有另一个按钮应该打开VC2,同时关闭VC1。


- (IBAction)buttonPressedFromVC1:(UIButton *)sender

{

    VC2 *vc2 = [[VC2 alloc] initWithNibName:@"VC2" bundle:nil];

    [vc2 setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];

    [self presentViewController:vc2 animated:YES completion:nil];

    [self dismissViewControllerAnimated:YES completion:nil];

} // This shows a warning: Attempt to dismiss from view controller <VC1: 0x715e460> while a presentation or dismiss is in progress!



- (IBAction)buttonPressedFromVC2:(UIButton *)sender

{

    [self dismissViewControllerAnimated:YES completion:nil];

} // This is going back to VC1. 

我希望它返回到主视图控制器,同时应从内存中永久删除VC1。仅当我单击主控制器上的VC1按钮时,才会显示VC1。


主视图控制器上的另一个按钮也应该能够直接绕过VC1来显示VC2,并且在VC2上单击一个按钮时应回到主控制器。没有长时间运行的代码,循环或任何计时器。只需裸露电话即可查看控制器。


慕村9548890
浏览 577回答 3
3回答

HUX布斯

我认为您误解了有关iOS模式视图控制器的一些核心概念。当您关闭VC1时,任何由VC1呈现的视图控制器也将被关闭。Apple希望模式视图控制器以堆叠的方式流动-在您的情况下,VC2由VC1提供。一旦您从VC1提供VC2,就将VC1解雇,这简直是一团糟。为了实现所需的功能,在VC1退出后,buttonPressedFromVC1应该立即使mainVC出现在VC2中。我认为,没有代表就可以实现这一目标。大致情况:UIViewController presentingVC = [self presentingViewController];[self dismissViewControllerAnimated:YES completion:&nbsp;^{&nbsp; &nbsp; [presentingVC presentViewController:vc2 animated:YES completion:nil];&nbsp;}];注意self.presentingViewController存储在其他变量中,因为在vc1关闭自身之后,您不应对其进行任何引用。

慕田峪9158850

Swift中的示例,描绘了上述代工厂的说明和Apple的文档:基于Apple的文档和上述铸造厂的说明(纠正了一些错误),使用委托设计模式的presentViewController版本:ViewController.swiftimport UIKitprotocol ViewControllerProtocol {&nbsp; &nbsp; func dismissViewController1AndPresentViewController2()}class ViewController: UIViewController, ViewControllerProtocol {&nbsp; &nbsp; @IBAction func goToViewController1BtnPressed(sender: UIButton) {&nbsp; &nbsp; &nbsp; &nbsp; let vc1: ViewController1 = self.storyboard?.instantiateViewControllerWithIdentifier("VC1") as ViewController1&nbsp; &nbsp; &nbsp; &nbsp; vc1.delegate = self&nbsp; &nbsp; &nbsp; &nbsp; vc1.modalTransitionStyle = UIModalTransitionStyle.FlipHorizontal&nbsp; &nbsp; &nbsp; &nbsp; self.presentViewController(vc1, animated: true, completion: nil)&nbsp; &nbsp; }&nbsp; &nbsp; func dismissViewController1AndPresentViewController2() {&nbsp; &nbsp; &nbsp; &nbsp; self.dismissViewControllerAnimated(false, completion: { () -> Void in&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let vc2: ViewController2 = self.storyboard?.instantiateViewControllerWithIdentifier("VC2") as ViewController2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.presentViewController(vc2, animated: true, completion: nil)&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; }}ViewController1.swiftimport UIKitclass ViewController1: UIViewController {&nbsp; &nbsp; var delegate: protocol<ViewControllerProtocol>!&nbsp; &nbsp; @IBAction func goToViewController2(sender: UIButton) {&nbsp; &nbsp; &nbsp; &nbsp; self.delegate.dismissViewController1AndPresentViewController2()&nbsp; &nbsp; }}ViewController2.swiftimport UIKitclass ViewController2: UIViewController {}基于上述代工厂的解释(更正了一些错误),使用委托设计模式的pushViewController版本:ViewController.swiftimport UIKitprotocol ViewControllerProtocol {&nbsp; &nbsp; func popViewController1AndPushViewController2()}class ViewController: UIViewController, ViewControllerProtocol {&nbsp; &nbsp; @IBAction func goToViewController1BtnPressed(sender: UIButton) {&nbsp; &nbsp; &nbsp; &nbsp; let vc1: ViewController1 = self.storyboard?.instantiateViewControllerWithIdentifier("VC1") as ViewController1&nbsp; &nbsp; &nbsp; &nbsp; vc1.delegate = self&nbsp; &nbsp; &nbsp; &nbsp; self.navigationController?.pushViewController(vc1, animated: true)&nbsp; &nbsp; }&nbsp; &nbsp; func popViewController1AndPushViewController2() {&nbsp; &nbsp; &nbsp; &nbsp; self.navigationController?.popViewControllerAnimated(false)&nbsp; &nbsp; &nbsp; &nbsp; let vc2: ViewController2 = self.storyboard?.instantiateViewControllerWithIdentifier("VC2") as ViewController2&nbsp; &nbsp; &nbsp; &nbsp; self.navigationController?.pushViewController(vc2, animated: true)&nbsp; &nbsp; }}ViewController1.swiftimport UIKitclass ViewController1: UIViewController {&nbsp; &nbsp; var delegate: protocol<ViewControllerProtocol>!&nbsp; &nbsp; @IBAction func goToViewController2(sender: UIButton) {&nbsp; &nbsp; &nbsp; &nbsp; self.delegate.popViewController1AndPushViewController2()&nbsp; &nbsp; }}ViewController2.swiftimport UIKitclass ViewController2: UIViewController {}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

iOS