【问题标题】:how to manage UIViewControllers which open and reopen each other如何管理相互打开和重新打开的 UIViewController
【发布时间】:2018-11-25 23:28:10
【问题描述】:

我有 2 个相互打开的 UIViewControllers,当它发生几次时我的问题就开始了,所以当用户点击关闭按钮时,这些 viewcontrollers 的实例太多了,

我没有使用 Segue,而是使用下面的代码

 let vc = self.storyboard?.instantiateViewController(withIdentifier: "verifySMSView") as! SMSVerificationViewController
            vc.delegate = self
            self.present(vc, animated: true, completion: nil)

这是显示我的问题的 Gif 文件 (LINK)

如何管理 viewcotrollers 并停止重新打开, 任何帮助将不胜感激

【问题讨论】:

  • 您能详细解释一下您想要实现的目标吗?以及你现在是如何解散控制器的?
  • 请检查链接(我已经在我的问题中提出)你会发现我的问题。想象一下我有 A 和 B 视图控制器,A 有一个按钮可以制作 B,B 有一个按钮可以制作 B做A,当我们做几次,所以我们有A的数量和B的数量,但我只想有一个实例!
  • 你真的要B再做A吗?还是您只想解雇 B 并返回 A?
  • 我想,都做对方,但一次!

标签: ios swift3 uiviewcontroller


【解决方案1】:

编写此代码

self.dismiss(animated: true, completion: nil)

“我已经有帐户”按钮的点击事件

【讨论】:

  • 我已经这样做了,但它不起作用,因为并让我得到“查看层次结构”的错误
  • 这里是: 其视图不在窗口层次结构中!
【解决方案2】:

我创建了一个简单的演示应用程序(没有故事板,只有代码),一切都按预期工作。将以下内容与您的代码进行比较:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        window = UIWindow()
        window?.rootViewController = ViewControllerA()
        window?.makeKeyAndVisible()
        return true
    }

}

class ViewControllerA: UIViewController {

    let createNewAccountButton: UIButton = {
        let button = UIButton(type: .system)
        button.translatesAutoresizingMaskIntoConstraints = false

        button.setTitle("Create new account", for: .normal)
        button.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)

        return button
    }()

    @objc func buttonTapped(_ sender: UIButton) {
        let viewControllerB = ViewControllerB()
        present(viewControllerB, animated: true, completion: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .lightGray

        view.addSubview(createNewAccountButton)

        NSLayoutConstraint.activate([
            createNewAccountButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            createNewAccountButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -20)
        ])
    }
}

class ViewControllerB: UIViewController {

    let alreadyHaveAnAccountButton: UIButton = {
        let button = UIButton(type: .system)
        button.translatesAutoresizingMaskIntoConstraints = false

        button.setTitle("I already have an account", for: .normal)
        button.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)

        return button
    }()

    @objc func buttonTapped(_ sender: UIButton) {
        dismiss(animated: true, completion: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .darkGray

        view.addSubview(alreadyHaveAnAccountButton)

        NSLayoutConstraint.activate([
            alreadyHaveAnAccountButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            alreadyHaveAnAccountButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -20)
        ])
    }
}

【讨论】:

  • 感谢安德烈,但这段代码不是我想要的! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-09
相关资源
最近更新 更多