字段在初始化后具有nil值,而不是此字段在初始化内部具有适当的值

时间:2019-04-28 11:18:08

标签: swift null init

我尝试在mainScreenViewController字段中设置值。在命令“ self.mainScreenViewController = mainScreenViewControler”之后的初始化函数内部,将设置该值(而不是nil)。在按下项目并调用函数tapSortItem之后,mainScreenViewController字段中的值为nil。为什么?有什么问题?

internal class MainScreenViewController: UITableViewController {                   
   private var mainScreenNavigationBarItems:   MainScreenNavigationBarItems?

   override func viewDidLoad() {
      super.viewDidLoad()
      mainScreenNavigationBarItems = MainScreenNavigationBarItems(self)
     }
 }



internal class MainScreenNavigationBarItems: UINavigationBar {
    private var mainScreenViewController: MainScreenViewController?

    init(_ mainScreenViewController: MainScreenViewController) {
        super.init(frame: CGRect())
        self.mainScreenViewController = mainScreenViewController
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    @IBAction func tapSortItem(_ sender: UIBarButtonItem) {
        print("Sort Item was tapped")
        let alert = UIAlertController(title: "My Alert", message: "This is an alert.", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Default action"), style: .default, handler: { _ in
        NSLog("The \"OK\" alert occured.")
    }))
        mainScreenViewController?.present(alert, animated: true, completion: nil)
   }
}

0 个答案:

没有答案