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)
    }
Indrajeet
  • 4,827
  • 2
  • 26
  • 42
  • 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)` – PK20 Nov 27 '15 at 08:58
  • To do same you need to use prototype method. – Viraj Padsala Nov 27 '15 at 09: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 – Mohammed Awad Nov 27 '15 at 09:12

4 Answers4

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 {

Community
  • 1
  • 1
Viraj Padsala
  • 1,368
  • 1
  • 11
  • 30
  • this is in my second controller [link](http://i.imgur.com/anvOueQ.png) ,, and this is my first controller [link](http://i.imgur.com/s31xZKf.png), 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 :) – Mohammed Awad Nov 27 '15 at 09:27
  • 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. http://stackoverflow.com/questions/26742138/singleton-in-swift – Oleh Kudinov Nov 27 '15 at 10:43
  • Use NSUserDefault as last option. – Viraj Padsala Nov 27 '15 at 12:00
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";
Oleh Kudinov
  • 2,303
  • 22
  • 27
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

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)
}