简体   繁体   English

如何从模态视图控制器将数据传递回视图控件

[英]How to pass data back to view control from modal view controller

so usually I would use a delegate pattern for this but this is a tricky situation. 所以通常我会为此使用委托模式,但这是一个棘手的情况。

View controller A presents -> view controller B which presents -> view controller C. 视图控制器A呈现->视图控制器B呈现->视图控制器C。

When the user finished the steps in view controller C, I will dismiss both B and C in one call with 当用户完成视图控制器C中的步骤后,我将在一次调用中关闭B和C

   self.presentingViewController?.presentingViewController?.dismiss(animated: true, completion: nil)

I would like to pass the data from view controller C back to A. How is this possible, since A has no reference to C, how can I implement a delegate? 我想将数据从视图控制器C传回给A。这怎么可能,由于A没有对C的引用,如何实现委托?

****EDIT: This is all done programmatically, so I cant use unwind segues ****编辑:这都是以编程方式完成的,所以我不能使用放松的顺序

***** SOLUTION ******* *****解决方案*******

The best solution I found was to just add an observer in VC A and post the object in VC C when dismissing the VC: 我发现的最佳解决方案是在VC中添加一个观察器,然后在关闭VC时将对象发布到VC C中:

self.presentingViewController?.presentingViewController?.dismiss(animated: true) {
        NotificationCenter.default.post(name: Notification.Name("UpdateKeywords"), object: self.account)
    }

Dont forget to remove the observer in VC A in deinit() 不要忘记在deinit()中删除VC A中的观察者

我通常要做的是在b中创建对A的引用,然后在C中创建对该引用的引用,然后在返回A之前,对引用进行更改,然后调用self.presentingViewController?.presentingViewController?.dismiss(animated: true, completion: nil)

you can use singleton design pattern. 您可以使用单例设计模式。 Declare a singleton class for datas. 声明一个单例类的数据。

class DatasSingleton{
    static let sharedInstance = DatasSingletonSingleton()
    var datas: int = 0
}

in class C you set data 在C类中,您设置数据

class C{
    func xyz(){
        DatasSingleton.sharedInstance.datas = 6
    }
}

in class A you can read data 在A类中,您可以读取数据

class A{
    func xyz(){
        print(DatasSingleton.sharedInstance.datas)
    }
}

The example code, VC A present VC B, VC B present VC C, at VC C you enter text, and then the method of VC A is called (here the data is transferred) with the text entered at VC C. See below... 示例代码VC A当前VC B,VC B当前VC C,在VC C上输入文本,然后调用VC A的方法(此处传输数据),并在VC C输入文本。请参见下文。 ..

Hope that helps 希望能有所帮助

(Xcode 8.1 (8B62): Delete storyboard, launchscreen, Clean properties Main story board and Launchscreen in Info.plist and replace AppDelegate.swift with the following content) (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.

相关问题 关闭时如何将数据从模态视图控制器传回 - How to pass data from modal view controller back when dismissed 如何将数据传递回堆栈中的视图控制器? - How Pass Data back a view controller in the stack? 如何将数据从第三视图控制器传递回第一视图控制器? - How Do I Pass Data from Third View Controller Back to First View Controller? 如何使用委托将数据从多个视图控制器传递回根视图控制器? IOS 7 - How to pass data back from multiple view controllers to root view controller using delegate? Ios 7 如何使用完成按钮将数据从视图传递回标签栏控制器中的视图 - How to pass data back from a view to a view in a tab bar controller with a done button 如何从模态回到主视图控制器 - How to get back from modal to main view controller 从Modal View Controller返回RootViewController - Back to RootViewController from Modal View Controller 如何将视图 controller 中的数据传递到表视图 controller? - How to pass the data from view controller to the table view controller? 委托从模态呈现的视图控制器传回数据 - Delegation to pass data back from modally presented view controller 从iPhone上的弹出视图控制器传回数据 - Pass data back from a popover view controller on iPhone
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM