-2

我是 Swift 新手,想做一个简单的应用程序

当用户启动应用程序时,他会看到FirstViewController有 1 个登录按钮,当用户点击此按钮时,应用程序会显示一个SecondViewController模态

SecondViewController有文本字段和“身份验证按钮”上,我希望通过点击“身份验证按钮”,被SecondViewController解雇并FirstViewController显示ThirdViewController

好的,我使用 self.navigationController.show() 方法

let secondVC = SecondViewController()
self.navigationController.show(secondVC, sender: self)

并关闭关闭VC的方法,但是当我尝试打开ThirdViewController之后dismiss,它不起作用

func buttonTapped() {
    let firstVC = FirstViewController()
    let thirdVC = ThirdViewController()
    firstVC.navigationController.show(thirdVC, sender: self)
    self.dismiss(animated: true, completion: nil)
    
}

怎么做才对?

我不使用情节提要,如果它很重要

4

1 回答 1

0

您是“Swift 新手” ……在尝试“制作一个简单的应用程序”之前,您可能想花更多时间学习基础知识。

问题是,在这一点上,您甚至不知道要问什么或正确的术语。

例如,你说:

  • “到达 FirstViewController” ……“到达”是什么意思?
  • “点击”而不是“点击”
  • SecondViewController关闭和FirstViewController打开ThirdViewController ...“关闭”和“打开”是模棱两可的术语

我并不是说听起来很苛刻,但你会在学习字母表之前尝试写小说吗?

但是,这是一个快速、非常简单的示例……

我们从FirstViewController根控制器开始...按钮点击呈现SecondViewController并设置一个闭包...在该控制器中的按钮点击调用闭包,此时我们传递用户输入的字符串并将根控制器替换为ThirdViewController

class FirstViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemRed
        
        // create a "Login" button
        let btn = UIButton()
        btn.backgroundColor = .darkGray
        btn.setTitleColor(.white, for: .normal)
        btn.setTitleColor(.lightGray, for: .highlighted)
        btn.setTitle("Login", for: [])
        btn.translatesAutoresizingMaskIntoConstraints = false
        
        view.addSubview(btn)
        
        let g = view.safeAreaLayoutGuide
        NSLayoutConstraint.activate([
            // center the button in the view
            btn.centerXAnchor.constraint(equalTo: g.centerXAnchor),
            btn.centerYAnchor.constraint(equalTo: g.centerYAnchor),
            btn.widthAnchor.constraint(equalTo: g.widthAnchor, multiplier: 0.75),
            btn.heightAnchor.constraint(equalToConstant: 50.0),
        ])
        
        // add touchUpInside action
        btn.addTarget(self, action: #selector(loginBtnTapped(_:)), for: .touchUpInside)
    }
    
    @objc func loginBtnTapped(_ sender: Any?) -> Void {
        let secondVC = SecondViewController()
        
        // set the closure
        secondVC.theClosure = { [weak self] str1, str2 in
            guard let self = self else { return }
            
            // closure was called from SecondVC,
            //  dismiss it and then show ThirdVC
            self.dismiss(animated: true, completion: {
                let thirdVC = ThirdViewController()
                
                // set the strings
                thirdVC.val1 = str1
                thirdVC.val2 = str2
                
                // replace First VC with Third VC
                UIApplication.shared.windows.first?.rootViewController = thirdVC
                UIApplication.shared.windows.first?.makeKeyAndVisible()
            })
        }
        
        // present the second VC
        self.present(secondVC, animated: true, completion: nil)
    }
    
    
}

class SecondViewController: UIViewController {

    var theClosure: ((String, String) -> ())?
    
    let tf1 = UITextField()
    let tf2 = UITextField()

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

        // stack view to arrange two fields and a button
        let stack = UIStackView()
        stack.axis = .vertical
        stack.spacing = 20
        stack.translatesAutoresizingMaskIntoConstraints = false

        // add text fields to stack view
        [tf1, tf2].forEach { tf in
            tf.borderStyle = .roundedRect
            stack.addArrangedSubview(tf)
        }
        
        // create an "Auth" button
        let btn = UIButton()
        btn.backgroundColor = .darkGray
        btn.setTitleColor(.white, for: .normal)
        btn.setTitleColor(.lightGray, for: .highlighted)
        btn.setTitle("Auth", for: [])
        
        // add button to stack view
        stack.addArrangedSubview(btn)

        view.addSubview(stack)
        
        let g = view.safeAreaLayoutGuide
        NSLayoutConstraint.activate([
            // constrain stack view centered horizontally, near top
            stack.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0),
            stack.centerXAnchor.constraint(equalTo: g.centerXAnchor),
            stack.widthAnchor.constraint(equalTo: g.widthAnchor, multiplier: 0.75),
            btn.heightAnchor.constraint(equalToConstant: 50.0),
        ])

        // add touchUpInside action
        btn.addTarget(self, action: #selector(authBtnTapped(_:)), for: .touchUpInside)
    }
    
    @objc func authBtnTapped(_ sender: Any?) -> Void {
        // get the text from the two fields
        var s1 = tf1.text ?? ""
        var s2 = tf2.text ?? ""
        if s1.isEmpty { s1 = "Field 1 Empty" }
        if s2.isEmpty { s2 = "Field 2 Empty" }

        // call the closure with the two strings
        theClosure?(s1, s2)
    }

}

class ThirdViewController: UIViewController {
    
    var val1: String = ""
    var val2: String = ""

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemBlue
        
        // create a multi-line label
        let label = UILabel()
        label.backgroundColor = .cyan
        label.numberOfLines = 0
        label.translatesAutoresizingMaskIntoConstraints = false
        
        view.addSubview(label)
        
        let g = view.safeAreaLayoutGuide
        NSLayoutConstraint.activate([
            // constrain the label centered in the view
            label.centerXAnchor.constraint(equalTo: g.centerXAnchor),
            label.centerYAnchor.constraint(equalTo: g.centerYAnchor),
            label.widthAnchor.constraint(equalTo: g.widthAnchor, multiplier: 0.75),
        ])

        // set label text
        var s: String = "User-entered strings:"
        s += "\n\n"
        s += val1
        s += "\n\n"
        s += val2

        label.text = s
    }
    
}
于 2021-12-06T15:38:08.733 回答