0

I have 2 controllers

and have got 1 global variable, the problem is if I go to controller 2 and click on button northAmericaClick, it will navigate back to control 1, but the value of global variable won't change!

this is my code

controller 1

class OurViewController: UIViewController {

    @IBOutlet weak var menuButton: UIBarButtonItem!

    @IBOutlet weak var selectedServer: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        selectedServer.setTitle(selected server, forState: UIControlState.Normal) // selected server this is global variable

}

controller 2

class selectServerController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

    }


    @IBAction func northAmericaClick(sender: AnyObject) {
        selectedserver = "North America"

                self.navigationController?.popViewControllerAnimated(true)
    }
3
  • 1
    If you want to set data in your parent view controller then probably you have to use unwind segue and not self.navigationController?.popViewControllerAnimated(true) Commented Nov 27, 2015 at 8:58
  • To do same you need to use prototype method. Commented Nov 27, 2015 at 9:08
  • i know how to use segue , but this is help me because there's more than 1 input and if he go to other controller with segue the inputs will get empty Commented Nov 27, 2015 at 9:12

4 Answers 4

2

From

You need to use a delegate. Here is an example how do use a delegate in Swift.

On your first ViewController, set your delegate when you load the second VC:

For example, if you are using the Storyboard Editor:

var secondViewController = (segue.destinationViewController.visibleViewController as  MySecondViewControllerClass)
secondViewController.delegate = self

Write a Protocol and define a func to write you values back

For example, create a file called "Protocol.swift" and write something like that:

protocol writeValueBackDelegate {
    func writeValueBack(value: String)
}

Add the function to your FirstViewController

func writeValueBack(value: String) {
   // this is my value from my second View Controller
}

And to your ViewControllerClass

class ViewController: UIViewController, writeValueBackDelegate

Go to the Second View Controller, and add the delegate here:

class SecondViewController: ViewController {

    // delegate for FirstViewController
    var delegate: writeValueBackDelegate?

On your Second View Controller, you can now use this to call the func in the first View Controller an pass data.

delegate?.writeValueBack("That is a value")

You also need to indicate that your first view controller implements the protocol: class ViewController: UIViewController, writeValueBackDelegate {

Sign up to request clarification or add additional context in comments.

3 Comments

this is in my second controller link ,, and this is my first controller link, when i click on select server it would go the second and show the images you it's like buttons each one got function if i click on it , it have to change the value of global variable , i don't think i need protocol to do that , but thanks :)
Personally I would do it with delegate. But you also have another option. Create a Model/Data class (singleton) for you view controllers and define the variable in it. Then access to this singleton from these view controllers. stackoverflow.com/questions/26742138/singleton-in-swift
Use NSUserDefault as last option.
0

A part of doing it with delegate you also can create singleton class ViewControllersDataModel class and share the variable using it:

import Foundation

class ViewControllersDataModel {
    static let sharedInstance = ViewControllersDataModel()

    var selectedserver: String = ""

    private init() {
    }
}

And call it like this:

ViewControllersDataModel.sharedInstance.selectedserver = "Selected Option";

1 Comment

Property 'self.selectedserver' not initialized at implicitly generated super.init call
0

Ok, I can do this with this code, only check when viewWillDisapear and call the parent of this view controller in the navicationController:

override func viewWillDisappear(animated: Bool) {
    if ((self.navigationController!.viewControllers.last?.isKindOfClass(ActivityMyViewController)) == true){
        let backView:MyViewController = self.navigationController!.viewControllers.last as! MyDetailViewController
        backView // do whatever you want
    }

}

I hope this code can help you, good luck

Comments

0

thanks guys for helping ;)

it was very simple

i just use then when it comeback ^^"

override func viewWillAppear(animated: Bool) {
    selectedServer.setTitle(selectedserv, forState: UIControlState.Normal)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.