【发布时间】:2016-07-05 20:45:09
【问题描述】:
我有 viewController 与名为“toSecond”的 secondViewController 的 segue。 在 viewController 我加载 customView.xib
let myCustomView = NSBundle.mainBundle().loadNibNamed("customView", owner: self, options: nil)[0]
在这个 customView 中,我有带有操作的按钮:
viewController().goToSecond()
在 viewController 我有这个代码的功能
func goToSecond() {
self.performSegueWithIdentifier("toSecond", sender: self)
}
但是当我在 customView 中按下按钮时,我变成了一个错误:
viewController 没有标识符为“toSecond”的segue
当我直接从 viewController 调用这个函数时,一切都很好!
那么,如何从我的 customView 调用 performSegueWithIdentifier?
customView源码:
import UIKit
class customView: UIView {
@IBAction func ToSecondButton(sender: AnyObject) {
viewController().goToSecond() }
}
viewController 源码:
import UIKit
class viewController: UIViewController {
...
let myCustomView = NSBundle.mainBundle().loadNibNamed("customView", owner: self, options: nil)[0]
self.view.addSubview(myCustomView)
func goToSecond() {
self.performSegueWithIdentifier("toSecond", sender: self)
}
...
}
【问题讨论】:
-
如果自定义视图的文件所有者是视图控制器,我不明白这段代码:
viewController().goToSecond()是如何包含在自定义视图中的。您能否为您的自定义视图提供更多源代码?您如何在自定义视图中创建对视图控制器的引用? -
我在展示viewController中展示了customView...我在viewController和secondViewController之间的segue...我可以直接从viewController调用func goToSecond(),但不能从customView调用!如果在函数 goToSecond 中写一些类似 print("Click!") 我在日志中看到这个点击!
-
以后,我鼓励你坚持使用Cocoa naming conventions,所有类名都以大写字母开头。这是不必要的混乱......