viewDidAppear中的代码未执行

时间:2018-06-07 20:29:18

标签: ios swift

我是初学者Swift用户,想要在我的应用启动后立即打开弹出窗口。为此,我使用以下代码:

import UIKit

class ViewController: UIViewController {
    func viewDidAppear(){
        if let vc = storyboard?.instantiateViewController(withIdentifier: 
      "PasswordPopUp") as? PasswordPopUp {
            vc.modalPresentationStyle = .overCurrentContext
            present(vc, animated: true, completion: nil)
        } else {
            print("error creating PasswordPopUp")
        }
    }
}

好像根本没有执行viewDidAppear中的代码。这可能是愚蠢的,但我做错了什么?我非常感谢你的帮助。

2 个答案:

答案 0 :(得分:3)

你的意思是viewDidAppear(_ animated: Bool)。说:

func viewDidAppear() {
}

您正在声明一种新方法,而不是viewDidAppear(_ animated: Bool)

您应该使用:

override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        // The rest of your code.
}

只需输入viewDidA,Xcode就会自动为您完成。

答案 1 :(得分:0)

正确的方法

override func viewDidAppear(_ animated:Bool){ 

  super.viewDidAppear(animated)

  if let vc = storyboard?.instantiateViewController(withIdentifier:"PasswordPopUp") as? PasswordPopUp {
      vc.modalPresentationStyle = .overCurrentContext
      present(vc, animated: true, completion: nil)
  } else {
    print("error creating PasswordPopUp")
   }


}