Call function from other ViewController

2019-03-02 08:59发布

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

FirstViewController.swift contains:

class FirstViewController: UIViewController {
    @IBAction func callFunctionInOtherClass(sender: AnyObject) {
//        Call "func showAlert" in SecondViewController when clicking the UIButton in FirstViewController
    }
}

SecondViewController.swift contains:

class SecondViewController: UIViewController {

    @IBOutlet weak var textField: UITextField!

    func showAlert(sender: AnyObject) {
        let alert = UIAlertView(title: "Working!", message: "This function was called from FirstViewController!\nTextField says: \(textField.text!)", delegate: nil, cancelButtonTitle: "Okay")
        alert.show()
    }
}

I want to be able to call the func showAlert() in SecondViewController when taping on the UIButton in FirstViewController.

I've already spent many nights to find a solution but none worked. Does anybody know what to do to reach this goal?

I uploaded a sample Xcode project here: CallFuntionInOtherClassX | filedropper.com

P.S.: Of course, I could post some code and explain what error I get, but I think it's not reasonable because I really don't know how to do that.

4条回答
姐就是有狂的资本
2楼-- · 2019-03-02 09:41

You may use NSNotificationCentre to accomplish this task.

In viewDidLoad method of your SecondViewController class register self as observer to receive notification broadcasts:-

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

and in FirstViewController's button action method you should fire the notification by writing :-

@IBAction func callFunctionInOtherClass(sender: AnyObject) {
    //Call "func showAlert" in SecondViewController when clicking the UIButton in FirstViewController
    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "callForAlert"), object: nil)
}

Don't forget to call removeObserver in SecondViewController's viewDidUnload method.

查看更多
ら.Afraid
3楼-- · 2019-03-02 09:43

Try this:

SecondViewController().showAlert(self)

In your second view controller

 if let text = textField?.text {
     dispatch_async(dispatch_get_main_queue(),{

      let alert = UIAlertView(title: "Working!", message: "This function was called from FirstViewController!\nTextField says: \(text)", delegate: nil, cancelButtonTitle: "Okay")
      alert.show()

    })
}
查看更多
闹够了就滚
4楼-- · 2019-03-02 09:48

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)
}
查看更多
▲ chillily
5楼-- · 2019-03-02 09:59

If you want to show alert view to second viewcontroller from firstview controller when go to it you can do something like,

  self.performSegueWithIdentifier("your segue identifier", sender: self)  // or whatever way if you are not using storyboard or segue

    let alert = UIAlertView(title: "Working!", message: "This function was called from FirstViewController!", delegate: nil, cancelButtonTitle: "Okay")
    alert.show()

If you want to set any variables of secondviewcontroller then you need to implement prepareForSegue method. (if you are using segue).

Second thing you can show alertview in viewDidload of secondViewController also.

查看更多
登录 后发表回答