简体   繁体   English

从2个View Controller传递数据而没有segue

[英]passing data from 2 view controllers without segue

I have a mainviewcontroller and a popup view controller which opens without a segue. 我有一个mainviewcontroller和一个弹出视图控制器,打开时没有提示。

the popup viewcontroller recive data from Firebase, and then i need to append this data to an array in the mainviewcontroller. 弹出的viewcontroller从Firebase接收数据,然后我需要将此数据附加到mainviewcontroller中的数组。

How can i do that? 我怎样才能做到这一点?

(i tried to create a property of the popupviewcontroller in the mainviewcontroller, but in crashes the app) (我试图在mainviewcontroller中创建popupviewcontroller的属性,但会导致应用崩溃)

this is the code that opens the popup: 这是打开弹出窗口的代码:

 @IBAction func showPopUp(_ sender: Any) {
        let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "sbPopUp") as! PopUpViewController
        self.addChild(popOverVC)
        popOverVC.view.frame = self.view.frame
        self.view.addSubview(popOverVC.view)
        popOverVC.didMove(toParent: self)

You need to connect the classes so that the popup class knows what to do with the data once it has been received. 您需要连接这些类,以便弹出类知道接收到数据后如何处理。 Here's a sample structure that works in a playground that you should be able to apply to your real classes. 这是一个在操场上工作的示例结构,您应该可以将其应用于实际课程。

class MainClass {

    func showPopUp() {
        let popOverVC = PopUpClass()
        popOverVC.update = addToArray
        popOverVC.receivedData()
    }

    func addToArray() {
        print("Adding")
    }
}

class PopUpClass {
    var update: (()->())?

    func receivedData() {
        if let updateFunction = update {
            updateFunction()
        }
    }
}

let main = MainClass()
main.showPopUp()

Or you could create a global variable so it can be accessed anywhere. 或者,您可以创建一个全局变量,以便可以在任何地方访问它。 ... It is better to pass data but I just thought it would be easier in this instance, so the variable can be accessed anywhere in your entire project. ...最好传递数据,但我只是认为在这种情况下会更容易,因此可以在整个项目的任何位置访问变量。

if it is just a notification, you can show it in an alert, but if you don't want to use an alert my offer to present another view controller is not like this , try my code : 如果只是通知,则可以在警报中显示它,但是如果您不想使用警报,则我提供的呈现另一个视图控制器的提议不是这样,请尝试以下代码:

//if you have navigation controller ->
let vc = self.storyboard?.instantiateViewController(
            withIdentifier: "storyboadrID") as! yourViewController
        self.navigationController?.pushViewController(vc, animated: true)
//if you don't use navigation controller ->
let VC1 = self.storyboard!.instantiateViewController(withIdentifier: "storyboadrID") as! yourViewController
            self.present(VC1, animated:true, completion: nil)

also if you want to pass any data or parameter to destination view controller you can put it like this before presenting your view controller : 另外,如果您要将任何数据或参数传递给目标视图控制器,则可以在呈现视图控制器之前将其放置为:

VC1.textView.text = "test"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Segue在View Controllers之间传递数据 - Passing data between View Controllers using Segue 通过segue在视图控制器之间传递数据 - Passing data between view controllers through segue 将没有Segue的数据从Container View传递到MainVC - Passing Data with without Segue from Container View to MainVC 在不使用Segue的情况下将数据从一个视图控制器传递到另一个 - Passing data from one view controller to another without using Segue 在Swift视图控制器之间传递数据不起作用的第一个Segue过渡 - Passing data between Swift view controllers not working on first segue transition 使用segue在情节提要中的视图控制器之间传递数据 - Passing data between view controllers in storyboards using segue 将数据传递给实例化的视图控制器而无需转场 - Passing data to instantiated view controller without segue 在多个视图控制器之间共享数据而无须隔离(Swift 3) - Sharing data between multiple view controllers without segue (Swift 3) 如何在不使用segue的情况下在视图控制器之间传递数据 - How to pass data between the view controllers without using segue 不断地在视图控制器之间传递数据而不会引起冲突? 使用SWIFT - Pass data between view controllers constantly without a segue? USING SWIFT
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM