为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

iOS
慕村9548890 2019-10-24 10:44:06
在过去的一周中,我为解决如何显示和消除多个视图控制器问题而烦恼不已。我创建了一个示例项目,并直接从该项目中粘贴代码。我有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上单击一个按钮时应回到主控制器。没有长时间运行的代码,循环或任何计时器。只需裸露电话即可查看控制器。
查看完整描述

3 回答

?
HUX布斯

TA贡献1876条经验 获得超6个赞

我认为您误解了有关iOS模式视图控制器的一些核心概念。当您关闭VC1时,任何由VC1呈现的视图控制器也将被关闭。Apple希望模式视图控制器以堆叠的方式流动-在您的情况下,VC2由VC1提供。一旦您从VC1提供VC2,就将VC1解雇,这简直是一团糟。为了实现所需的功能,在VC1退出后,buttonPressedFromVC1应该立即使mainVC出现在VC2中。我认为,没有代表就可以实现这一目标。大致情况:


UIViewController presentingVC = [self presentingViewController];

[self dismissViewControllerAnimated:YES completion:

 ^{

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

 }];

注意self.presentingViewController存储在其他变量中,因为在vc1关闭自身之后,您不应对其进行任何引用。


查看完整回答
反对 回复 2019-10-24
?
慕田峪9158850

TA贡献1794条经验 获得超7个赞

Swift中的示例,描绘了上述代工厂的说明和Apple的文档:


基于Apple的文档和上述铸造厂的说明(纠正了一些错误),使用委托设计模式的presentViewController版本:

ViewController.swift


import UIKit


protocol ViewControllerProtocol {

    func dismissViewController1AndPresentViewController2()

}


class ViewController: UIViewController, ViewControllerProtocol {


    @IBAction func goToViewController1BtnPressed(sender: UIButton) {

        let vc1: ViewController1 = self.storyboard?.instantiateViewControllerWithIdentifier("VC1") as ViewController1

        vc1.delegate = self

        vc1.modalTransitionStyle = UIModalTransitionStyle.FlipHorizontal

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

    }


    func dismissViewController1AndPresentViewController2() {

        self.dismissViewControllerAnimated(false, completion: { () -> Void in

            let vc2: ViewController2 = self.storyboard?.instantiateViewControllerWithIdentifier("VC2") as ViewController2

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

        })

    }


}

ViewController1.swift


import UIKit


class ViewController1: UIViewController {


    var delegate: protocol<ViewControllerProtocol>!


    @IBAction func goToViewController2(sender: UIButton) {

        self.delegate.dismissViewController1AndPresentViewController2()

    }


}

ViewController2.swift


import UIKit


class ViewController2: UIViewController {


}

基于上述代工厂的解释(更正了一些错误),使用委托设计模式的pushViewController版本:

ViewController.swift


import UIKit


protocol ViewControllerProtocol {

    func popViewController1AndPushViewController2()

}


class ViewController: UIViewController, ViewControllerProtocol {


    @IBAction func goToViewController1BtnPressed(sender: UIButton) {

        let vc1: ViewController1 = self.storyboard?.instantiateViewControllerWithIdentifier("VC1") as ViewController1

        vc1.delegate = self

        self.navigationController?.pushViewController(vc1, animated: true)

    }


    func popViewController1AndPushViewController2() {

        self.navigationController?.popViewControllerAnimated(false)

        let vc2: ViewController2 = self.storyboard?.instantiateViewControllerWithIdentifier("VC2") as ViewController2

        self.navigationController?.pushViewController(vc2, animated: true)

    }


}

ViewController1.swift


import UIKit


class ViewController1: UIViewController {


    var delegate: protocol<ViewControllerProtocol>!


    @IBAction func goToViewController2(sender: UIButton) {

        self.delegate.popViewController1AndPushViewController2()

    }


}

ViewController2.swift


import UIKit


class ViewController2: UIViewController {


}


查看完整回答
反对 回复 2019-10-24
  • 3 回答
  • 0 关注
  • 576 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信