我有两个 ViewController,FirstViewController
and SecondViewController
。两者都有自己的 Swift 文件,FirstViewController.swift
and SecondViewController.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()
in SecondViewController
当贴在UIButton
in FirstViewController
.
我已经花了很多个夜晚来寻找解决方案,但没有任何效果。有谁知道要做什么才能实现这个目标?
我在这里上传了一个示例 Xcode 项目:CallFuntionInOtherClassX | 调用函数filedropper.com
P.S.:当然,我可以发布一些代码并解释我得到的错误,但我认为这是不合理的,因为我真的不知道该怎么做。