【问题标题】:ViewController dismiss swift iosViewController 关闭 swift ios
【发布时间】:2017-03-24 18:56:52
【问题描述】:

您好,我的英语不是很好,我提前道歉。

我的申请有问题。场景如下我将我的 rootViewController 分配给我的 ViewController 控制器,这是我的开始。我还有其他屏幕是描述屏幕,其中有两个登录和注册按钮,预加载时会将我带到他们的控制器。

现在,当我在日志全屏表单上并发送解雇命令时:

ViewController 注册

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

一切正常,视图被隐藏,但是当进入前一个屏幕时,如果有解除命令,我会验证是否已经有用户:

ViewController 描述应用

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

但它不执行操作。

代码

UIViewController

class ViewController: UIViewController {

    override func viewDidLoad() {

        FIRAuth.auth()!.addStateDidChangeListener() { auth, user in
            if user == nil {
                let descriptionController = DescriptionController()
                present(descriptionController, animated: true, completion: nil)
            }

        }
    }
}

描述控制器

class DescriptionController: UIViewController {

    @IBOutlet weak var sign_in_custom: UIButton!

    override func viewDidLoad() {

        FIRAuth.auth()!.addStateDidChangeListener() { auth, user in
            if user != nil {
               self.dismiss(animated: false, completion: nil)
            }

        }

        sign_in_custom.addTarget(self, action: #selector(changeToSingIn), for: [.touchUpInside])
    }

    func changeToSingIn() {
        let singInController = SingInController()
        present(singInController, animated: true, completion: nil)
    }
}

SingInController

class SingInController: UIViewController {
    @IBOutlet weak var sign_in_custom: UIButton!
    override func viewDidLoad() {
        sign_in_custom.addTarget(self, action: #selector(singIn), for: [.touchUpInside])
    }
    func showLoad() {
        let alert = UIAlertController(title: nil, message: "Please wait...", preferredStyle: .alert)
        alert.view.tintColor = UIColor.black
        let loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRectMake(10, 5, 50, 50) ) as UIActivityIndicatorView
        loadingIndicator.hidesWhenStopped = true
        loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
        loadingIndicator.startAnimating();
        alert.view.addSubview(loadingIndicator)
        present(alert, animated: true, completion: nil)
    }
    func hideLoad() {
        self.dismiss(animated: false, completion: nil)
    }
    func singIn() {
        if (emailVarification()){
            if (passwordVarification()){
                showLoad()
                guard let email = emailTextField.text else { return }
                guard let password = passwordTextField.text else { return }
                FIRAuth.auth()?.createUser(withEmail: email, password: password) { (user, error) in
                    hideLoad()
                    if (user != nil) {
                        self.dismiss(animated: false, completion: nil)
                    } else {
                        let alert = UIAlertController(title: "Error", message: "This is a error", preferredStyle: UIAlertControllerStyle.alert)
                        alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
                        self.present(alert, animated: true, completion: nil)
                    }
                }
            } else {
                let alert = UIAlertController(title: "Error", message: "This is a error", preferredStyle: UIAlertControllerStyle.alert)
                alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
                self.present(alert, animated: true, completion: nil)
            }
        } else {
            let alert = UIAlertController(title: "Error", message: "This is a error", preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
            self.present(alert, animated: true, completion: nil)
        }
    }
}

sequence

【问题讨论】:

    标签: ios iphone swift uiviewcontroller


    【解决方案1】:

    这是因为第二个控制器基本上只是放在现有控制器的顶部。第一个视图仍在第二个视图下运行,当第二个视图关闭时,第一个视图不会调用 ViewDidLoad。因此,要解决它,您可能希望将其添加到 ViewDidAppear 函数中。

    【讨论】:

      【解决方案2】:

      ViewdidAppear中使用此代码:

      FIRAuth.auth()!.addStateDidChangeListener() { auth, user in
              if user != nil {
                 self.dismiss(animated: false, completion: nil)
              }
      
          }
      
          sign_in_custom.addTarget(self, action: #selector(changeToSingIn), for: [.touchUpInside])
      

      【讨论】:

        【解决方案3】:

        与其让 DescriptionController 自行关闭,更好的方法是通知 ViewController 用户已登录,并在必要时返回任何错误。然后 ViewController 可以根据登录成功或失败执行所需的任何其他步骤。这可以通过使用委托模式来完成(DescriptionController 定义了一个协议,而 ViewController 实现了它)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-12-19
          • 2016-09-17
          • 2012-04-30
          • 1970-01-01
          • 2019-05-04
          • 2014-08-31
          • 2021-07-24
          • 1970-01-01
          相关资源
          最近更新 更多