感谢Austin解决了我的问题,现在更新了正确的代码: (以下代码描述了问题:)
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(self.myViewController2)
在MyViewController1中并在someFunction()中调用self.dismisscontroller(self)在MyViewController2中解除..
问题是委托是无...我不知道如何传递对委托函数的引用?
当调用另一个viewcontroller bye segue时,在segue中设置对委托函数的引用。没有segue为委托函数提供参考?
任何?
答案 0 :(得分:0)
在MyViewController2的viewDidLoad()
中,获取presentingViewController
并将其设置为委托。
// viewDidLoad - MyViewController2
if let pvc = self.presentingViewController as? MyViewController1 {
self.delegate = pvc
}