从其他ViewController

时间:2016-09-05 10:06:45

标签: ios swift class uiviewcontroller

我有两个ViewControllers,FirstViewControllerSecondViewController。两者都有自己的Swift文件,FirstViewController.swiftSecondViewController.swift

FirstViewController.swift包含:

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

SecondViewController.swift包含:

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()
    }
}

我希望在点击func showAlert()中的SecondViewController时,可以在UIButton中致电FirstViewController

我已经花了很多个夜晚才找到解决方案但没有工作。有人知道该怎么做才能达到这个目标吗?

我在这里上传了一个示例Xcode项目:CallFuntionInOtherClassX | filedropper.com

P.S。:当然,我可以发布一些代码并解释我得到的错误,但我认为这是不合理的,因为我真的不知道该怎么做。

4 个答案:

答案 0 :(得分:6)

您可以使用NSNotificationCentre来完成此任务。

viewDidLoad班级的SecondViewController方法注册自己作为接收通知广播的观察员: -

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

并在FirstViewController的按钮操作方法中,您应该通过以下方式触发通知: -

@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)
}

不要忘记在SecondViewController的removeObserver方法中调用viewDidUnload

答案 1 :(得分:1)

  

编辑:这些功能已在swift 3中进行了如下修改:

FirstViewController中的代码

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

SecondViewController中的代码:

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

答案 2 :(得分:0)

试试这个:

SecondViewController().showAlert(self)

在您的第二个视图控制器中

 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()

    })
}

答案 3 :(得分:0)

如果您希望在转到second viewcontroller后向firstview controller显示提醒视图,则可以执行以下操作:

  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()

如果您想设置variables的{​​{1}},则需要实施secondviewcontroller方法。 (如果你使用segue)。

您可以在prepareForSegue的{​​{1}}中显示提醒视图。