Parameter step of a viewController son to parent after doing a dismiss

2

From a parent controller I call another child ( popoverContent ) with self.present , the child driver is a typical view in the form of a popup that I end up closing with a dismiss but I want to pass some data to the parent view.

Call within the parent driver code:

self.present(popoverContent, animated: true, completion: nil ) 

I close the daughter view ( popoverContent ) with an action of a button

@IBAction func exitButtonAction(_ sender: Any) {
        self.dismiss(animated: true, completion: nil)
    }
  

How can I pass a parameter of type String, or Int, or dictionary to   the parent view after closing the daughter view with dismiss?

    
asked by Popularfan 31.01.2018 в 00:10
source

2 answers

1

The most logical thing to do is make a protocol, you can look at this: protocols, pass information from other views to one from an action

But if what you want is something simpler, and you are going to pass a couple of variables, you can apply the same concept as a delegate. In popoverContent you create a var that is of the father type, you create the popoverContent, to that variable you assign the value of the father, the samples and before closing it, popoverContent has a reference to the father, so it could modify the variable of the father that Would you like to.

In the popoverContent:

var padre: ClaseQueMeLlama?

In father, you create and assign:

//init del popoverContent
let popoverContent = popoverContent(........)
popoverContent.padre = self
self.present(popoverContent, animated: true, completion: nil ) 

For when you are going to close the popover:

@IBAction func exitButtonAction(_ sender: Any) {
    //aqui le pasas los valores que quieras al padre, que tendrán que estar definidos y ser public
    self.padre?.foo = self.foo
    self.dismiss(animated: true, completion: nil)
}
    
answered by 01.02.2018 / 16:52
source
1

Passing data from a child ViewController to a parent can be done by a protocol hosted on the parent and "manipulated" by the child but I think it can be somewhat annoying to implement these functions.

One way you can manipulate your variables is: 1. Create a class in which you store the data you need. 2. Create a static variable to access that class from any controller without the need to create a new object of this same class. 3. Implement set and get to access your data.

Something like this:

class almacenamiento: NSObject
{
    static let sharedInstance = almacenamiento()
    var dato1: String?

    override init() {
        super.init()
    }

    public func set_value(nuevoValor: String)
    {
        self.dato1 = nuevoValor
    }

    public func get_value() -> String?
    {
        return self.dato1
    }
}

The way you can retrieve the information is, for example, from the viewWillAppear () method:

override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        almacenamiento.sharedInstance.set_value(nuevoValor: "Hola mundo")
        if let value = almacenamiento.sharedInstance.get_value()
        {
            print("\nValor por medio de metodo get: \(value)")
        }

        if let value = almacenamiento.sharedInstance.dato1
        {
            print("Valor extraido directamente de la clase: \(value)\n")
        }
    }

Print: Value through method get: Hello world Value extracted directly from the class: Hello world

    
answered by 01.02.2018 в 15:20