【发布时间】:2016-09-05 10:06:45
【问题描述】:
我有两个 ViewController,FirstViewController 和 SecondViewController。两者都有自己的 Swift 文件,FirstViewController.swift 和 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()
}
}
我希望能够在点击FirstViewController 中的UIButton 时调用SecondViewController 中的func showAlert()。
我已经花了很多晚上来寻找解决方案,但都没有奏效。有人知道如何实现这个目标吗?
我在这里上传了一个示例 Xcode 项目:CallFuntionInOtherClassX | filedropper.com
P.S.:当然,我可以发布一些代码并解释我得到了什么错误,但我认为这不合理,因为我真的不知道该怎么做。
【问题讨论】:
-
为什么需要这个?这是确切的用例(即显示警报)吗?如果不是,实际用例是什么?
-
不分配 secondviewcontroller 如何从 firstviewcontroller 调用 showAlert() 方法?
-
使用委托或通知
-
@Losiowaty 不,这不是确切的用例。真实情况是:我有一个带有两个 ContainerViews 的 ViewController,我希望能够例如通过点击 Main/Parent-ViewController 中的按钮提交填写在 ContainerView 中的表单,因为它具有导航按钮、标题……
-
@Poles 我在编程方面不是很有经验。或许你能帮帮我?
标签: ios swift class uiviewcontroller