0
THX奧斯汀誰解決我的問題,更新的正確的代碼現在: (下面的代碼說明了問題:)委託功能,如何傳遞參考?
import Cocoa
class MyViewController1 : NSViewController, myProtocol {
// Identifier MyViewController2ID is set in storyboard for class MyViewController2
lazy var myViewController2: NSViewController = {
return self.storyboard!.instantiateControllerWithIdentifier("MyViewController2ID")
as! NSViewController
}()
// ViewController conforms to protocol
func myFunction() {
// do something
}
// ... some action
// presenting new viewcontroller as sheet
// new controller is presented and is dismissed
// when self.dismissController(self) is called
// in MyViewController2
self.presentViewControllerAsSheet(self.myViewController2)
}
protocol myProtocol : class {
func myFunction()
}
// Identifier MyViewController2ID for class
// MyViewController2 is set in storyboard
class MyViewController2 : NSViewController {
weak var delegate:myProtocol?
override func viewDidLoad() {
super.viewDidLoad()
...
if let pvc = self.presentingViewController as? MyViewController1 {
self.delegate = pvc
}
}
// function is activated
func someFunction() {
self.delegate?.myFunction()
self.dismissController(self)
}
}
MyViewController2是通過調用提出:
self.presentViewControllerAsSheet(自我。 myViewController2)
內MyViewController1,並通過調用self.dismisscontroller(個體經營)someFunction()在MyViewController2被解僱..
問題是委託是零...我不知道如何將引用傳遞給委託函數?
當調用另一個viewcontroller bye segue時,在segue中設置委託函數的引用。有沒有segue爲委託函數提供參考?
有人嗎?