Call function from other ViewController

前端 未结 4 1667
深忆病人
深忆病人 2021-01-24 15:31

I have two ViewControllers, FirstViewController and SecondViewController. Both have an own Swift file, FirstViewController.swift and

4条回答
  •  隐瞒了意图╮
    2021-01-24 16:10

    EDIT: These functions have been revised in swift 3 as follows:

    Code in FirstViewController

     override function viewDidLoad(){
    NotificationCenter.default.addObserver(self, selector: #selector(showAlert), name: NSNotification.Name(rawValue: "showAlert"), object: nil)
    }
    

    Code in SecondViewController:

    @IBAction func callFunctionInOtherClass(sender: AnyObject) {
     NotificationCenter.default.post(name: NSNotification.Name(rawValue: "showAlert"), object: nil)
    }
    

提交回复
热议问题