代码之家  ›  专栏  ›  技术社区  ›  willow

如何管理彼此打开和重新打开的UIViewControllers

  •  0
  • willow  · 技术社区  · 7 年前

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

    我不使用Segue,使用下面的代码

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

    LINK )

    如何管理viewcotroller和停止重新打开, 任何帮助都将不胜感激

    2 回复  |  直到 7 年前
        1
  •  0
  •   iOS Lifee    7 年前

    写这个代码

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

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

        2
  •  0
  •   André Slotta    7 年前

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

    @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)
            ])
        }
    }
    
    推荐文章