从扩展中获取 ViewController 的实例
Get Instance of ViewController from within extension
我想从 extension[=21] ViewController access =] 然后覆盖库中的函数,该函数应更改变量或从特定 ViewController(在本例中为 "ViewController")调用方法。
我该怎么做?或者有更推荐的选择吗?
(提示:我不想实例化一个新的 VC)
import PopupDialog
class ViewController: UIViewController {
//e.g. variable and method to access from extension
var score: Int = 0;
func startGame(){
...
}
}
extension PopupDialog{
open override func viewWillDisappear(_ animated: Bool) {
//change variables
//maybe also invoke methods
}
}
您可以使用 Notification 或 Protocol 在 ViewControllers
之间进行通信
通知示例:
创建一个扩展以使用通知名称:
extension Notification.Name {
static let ScoreUpdateNotification = Notification.Name("ScoreUpdateNotification")
}
在 First ViewController 的 DidLoad
中添加 Observer 方法
NotificationCenter.default.addObserver(self, selector: #selector(triggerThisMethod), name: Notification.Name.ScoreUpdateNotification, object: nil)
@objc func triggerThisMethod(_ notification: NSNotification) {
// When the notification arrives, this method will be called
if let object = notification.object {
// If there is Object passed with notification you will get it here.
}
}
到Post通知
let objectToPass = 23
NotificationCenter.default.post(name: .ScoreUpdateNotification, object: objectToPass)
协议
在 ViewController B
中创建协议和弱变量
protocol ProtocolB: class {
func didUpdateScore(_ score: Int)
}
weak var delegate: ProtocolB?
当你 Present/Push ViewController B 来自 A
let viewControllerB = // ...
viewControllerB.delegate = self
//present viewControllerB
将协议方法添加到 ViewController A
extension ViewController: ProtocolA {
func didUpdateScore(_ score: Int) {
// Do things
}
}
触发方法调用:
delegate?.didUpdateScore(25)
我想从 extension[=21] ViewController access =] 然后覆盖库中的函数,该函数应更改变量或从特定 ViewController(在本例中为 "ViewController")调用方法。
我该怎么做?或者有更推荐的选择吗?
(提示:我不想实例化一个新的 VC)
import PopupDialog
class ViewController: UIViewController {
//e.g. variable and method to access from extension
var score: Int = 0;
func startGame(){
...
}
}
extension PopupDialog{
open override func viewWillDisappear(_ animated: Bool) {
//change variables
//maybe also invoke methods
}
}
您可以使用 Notification 或 Protocol 在 ViewControllers
之间进行通信通知示例:
创建一个扩展以使用通知名称:
extension Notification.Name {
static let ScoreUpdateNotification = Notification.Name("ScoreUpdateNotification")
}
在 First ViewController 的 DidLoad
中添加 Observer 方法NotificationCenter.default.addObserver(self, selector: #selector(triggerThisMethod), name: Notification.Name.ScoreUpdateNotification, object: nil)
@objc func triggerThisMethod(_ notification: NSNotification) {
// When the notification arrives, this method will be called
if let object = notification.object {
// If there is Object passed with notification you will get it here.
}
}
到Post通知
let objectToPass = 23
NotificationCenter.default.post(name: .ScoreUpdateNotification, object: objectToPass)
协议
在 ViewController B
中创建协议和弱变量protocol ProtocolB: class {
func didUpdateScore(_ score: Int)
}
weak var delegate: ProtocolB?
当你 Present/Push ViewController B 来自 A
let viewControllerB = // ...
viewControllerB.delegate = self
//present viewControllerB
将协议方法添加到 ViewController A
extension ViewController: ProtocolA {
func didUpdateScore(_ score: Int) {
// Do things
}
}
触发方法调用:
delegate?.didUpdateScore(25)