[英]How to pass data back to view control from modal view controller
所以通常我會為此使用委托模式,但這是一個棘手的情況。
視圖控制器A呈現->視圖控制器B呈現->視圖控制器C。
當用戶完成視圖控制器C中的步驟后,我將在一次調用中關閉B和C
self.presentingViewController?.presentingViewController?.dismiss(animated: true, completion: nil)
我想將數據從視圖控制器C傳回給A。這怎么可能,由於A沒有對C的引用,如何實現委托?
****編輯:這都是以編程方式完成的,所以我不能使用放松的順序
*****解決方案*******
我發現的最佳解決方案是在VC中添加一個觀察器,然后在關閉VC時將對象發布到VC C中:
self.presentingViewController?.presentingViewController?.dismiss(animated: true) {
NotificationCenter.default.post(name: Notification.Name("UpdateKeywords"), object: self.account)
}
不要忘記在deinit()中刪除VC A中的觀察者
我通常要做的是在b中創建對A的引用,然后在C中創建對該引用的引用,然后在返回A之前,對引用進行更改,然后調用self.presentingViewController?.presentingViewController?.dismiss(animated: true, completion: nil)
您可以使用單例設計模式。 聲明一個單例類的數據。
class DatasSingleton{
static let sharedInstance = DatasSingletonSingleton()
var datas: int = 0
}
在C類中,您設置數據
class C{
func xyz(){
DatasSingleton.sharedInstance.datas = 6
}
}
在A類中,您可以讀取數據
class A{
func xyz(){
print(DatasSingleton.sharedInstance.datas)
}
}
示例代碼VC A當前VC B,VC B當前VC C,在VC C上輸入文本,然后調用VC A的方法(此處傳輸數據),並在VC C輸入文本。請參見下文。 ..
希望能有所幫助
(Xcode 8.1(8B62):在Info.plist中刪除情節提要,啟動屏幕,清理屬性主情節提要和啟動屏幕,並將AppDelegate.swift替換為以下內容)
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window!.rootViewController = Avc()
self.window!.makeKeyAndVisible()
return true
}
}
class Avc: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let b = self.view.addSubviewWithConstraints(["b" : Button(title: "Open B VC")], constraints: ["V:|-10-[b(70)]", "H:|-10-[b]-10-|"])["b"]
(b as! Button).action = {() in
let bvc = Bvc()
bvc.action = self.receiveData
self.present(bvc, animated: false,completion: nil)
}
}
func receiveData(data: String?) {
print(data)
}
}
class Bvc: UIViewController {
var action: ((_ data: String?)->())!
override func viewDidLoad() {
super.viewDidLoad()
let b = self.view.addSubviewWithConstraints(["b" : Button(title: "Open C VC")], constraints: ["V:|-10-[b(70)]", "H:|-10-[b]-10-|"])["b"]
(b as! Button).action = {() in
let cvc = Cvc()
cvc.action = self.action
self.present(cvc, animated: false,completion: nil)
}
}
}
class Cvc: UIViewController {
var action: ((_ data:String?)->())!
override func viewDidLoad() {
super.viewDidLoad()
let t = self.view.addSubviewWithConstraints(["t" : UITextField()], constraints: ["V:|-50-[t(24)]", "H:|-10-[t]-10-|"])["t"]
(t as! UITextField).backgroundColor = UIColor.white
let b = self.view.addSubviewWithConstraints(["b" : Button(title: "Transfer data to A VC")], constraints: ["V:|-100-[b(90)]", "H:|-10-[b]-10-|"])["b"]
(b as! Button).action = {() in
self.presentingViewController?.dismiss(animated: true, completion: nil)
self.presentingViewController?.presentingViewController?.dismiss(animated: true, completion: nil)
self.action((t as! UITextField).text)
}
}
}
class Button: UIButton {
var action: (()->())!
init(title: String) {
super.init(frame : CGRect.zero)
self.setTitle(title, for: UIControlState())
self.addTarget(self, action: #selector(Button.buttonAction(_:)), for: .touchUpInside)
}
required init?(coder: NSCoder) {super.init(coder: coder)}
func buttonAction(_ sender: AnyObject) {if action != nil {action()}}
}
extension UIView {
func addSubviewWithConstraints(_ views: [String : AnyObject], constraints: Array<String>) -> [String : AnyObject] {
for (_, view) in views {
self.addSubview(view as! UIView)
(view as! UIView).translatesAutoresizingMaskIntoConstraints = false
}
for i in 0 ..< constraints.count {self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: constraints[i], options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))}
return views
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.