gpt4 book ai didi

ios - 模态视图 Controller - 如何显示和关闭

转载 作者:行者123 更新时间:2023-12-01 18:12:51 24 4
gpt4 key购买 nike

在过去的一周里,我一直在思考如何通过显示和关闭多个 View Controller 来解决这个问题。我创建了一个示例项目并直接从项目中粘贴代码。我有 3 个 View Controller 及其相应的 .xib 文件。 MainViewController、VC1 和 VC2。我在主视图 Controller 上有两个按钮。

- (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.

我希望它回到主视图 Controller ,同时 VC1 应该已经从内存中删除了。只有当我单击主 Controller 上的 VC1 按钮时,VC1 才会出现。

主视图 Controller 上的另一个按钮也应该能够绕过 VC1 直接显示 VC2,并且当单击 VC2 上的按钮时应该返回到主 Controller 。没有长时间运行的代码、循环或任何计时器。只是简单地调用 View Controller 。

最佳答案

中的示例 swift ,描绘了上面代工厂的解释和苹果的文档:

  • 基于Apple's documentation和类型转换厂的
    上面的解释(纠正一些错误),presentViewController
    使用委托(delegate)设计模式的版本:

  • 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 {

    }
  • 根据上面代工厂的解释(纠正一些错误),
    使用委托(delegate)设计模式的 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 {

    }

    关于ios - 模态视图 Controller - 如何显示和关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14907518/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com