0

For example I've got 2 controllers that already in memory (Class 1, Class 2). How can I access Class 1 data from Class 2?

class Class_1: UIViewController {

    var number:UInt8 = 1

    override func viewDidAppear(animated: Bool) {
        number = 8
    }   
}

How do I access number variable in Class 2 and print it value? The point is to not make new instance, the point is to get pointer for Class 1 in memory and get access to it's data.

nhgrif
  • 61,578
  • 25
  • 134
  • 173
Alex
  • 3
  • 1
  • 2
  • Do you try delegate protocol? – Muzahid Mar 27 '16 at 09:34
  • I'm pretty new to swift, can you show me an example? – Alex Mar 27 '16 at 09:37
  • See [e.g. this answer for how to pass data between two different view controllers using segues](http://stackoverflow.com/a/35314768/4573247). – dfrib Mar 27 '16 at 09:43
  • 1
    [Required reading](https://developer.apple.com/library/mac/documentation/General/Conceptual/CocoaEncyclopedia/Model-View-Controller/Model-View-Controller.html#//apple_ref/doc/uid/TP40010810-CH14) on the subject. There is a lot to read, but without a solid understanding of MVC one stands no chance of developing a good iOS app. – Sergey Kalinichenko Mar 27 '16 at 09:46

1 Answers1

0

First of all change the name of your ViewController from Class_1 to Class1ViewController and Class_2 to Class2ViewController

You need to set the variable of Class2ViewController while intializing it in Class1ViewController, and to pass the data back from Class2ViewController to Class1ViewController you need to use delegate

For data transfer Class1ViewController to Class2ViewController, Open your Class1ViewController file and add the following prepareForSegue method if you are using storyboard

class Class1ViewController: UIViewController{
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "Class2ViewController" {
            if let class2ViewController = segue.destinationViewController as? Class2ViewController {
                class2ViewController.inforFromClass1 = "Class 2 Variable set from Class 1"
            }
        }
    }
}

However, If you are using xib and have a button for moving to Class2ViewController from Class1ViewController, following code should be written in IBAction of button that triggers Class2ViewController

class Class1ViewController: UIViewController{
    @IBAction showClass2ViewController(){
    let secondViewController = SecondViewController(nibName:"SecondViewController", bundle: NSBundle.mainBundle())
    secondViewController.infoFromClass1 = "Class 2 Variable set from Class 1"
    self.showViewController(secondViewController, sender: self)
   }
}

This is how you set the variable on Class2ViewController while showing it from Class1ViewController

Now to pass message from Class2ViewController to Class1ViewController you need to use delegates. Open you Class2ViewController and add the following protocol at the top

@objc protocol Class2ViewContollerDelegate :class{
   func printMessageFromClass2ViewController()
}

class Class2ViewController: UIViewController {   
}

Add a weak reference to the delagate in Class2ViewController class and call it in its ViewDidAppear or any other method you like,

@objc protocol Class2ViewContollerDelegate :class{
   func printMessageFromClass2ViewController()
}

class Class2ViewController: UIViewController {
   weak var delegate: Class2ViewControllerDelegate?
   override func viewDidAppear(animated: Bool) {
        self.delegate?.printValueFromClass2ViewController
   }   
}

Now that we have define the protocol in Class2ViewController we need to implement it in Class1ViewController. Back In your Class1ViewController file implement the protocol like this

class Class1ViewController: UIViewController, Class2ViewControllerDelegate {
    func printMessageFromClass2ViewController(){
        print("hey I just printed message in Class1ViewController through its delegate in Class2ViewController")
    }

    // For Xibs
    @IBAction showClass2ViewController(){
        let secondViewController = SecondViewController(nibName: "SecondViewController", bundle: NSBundle.mainBundle())
        secondViewController.infoFromClass1 = "Class 2 Variable set from Class 1"
        self.showViewController(secondViewController, sender: self)
    }

    //  For storyboards
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "Class2ViewController" {
            if let class2ViewController = segue.destinationViewController as?Class2ViewController {
                class2ViewController.inforFromClass1 = "Class 2 Variable set from Class 1"
            }
        }
    }
}
hariszaman
  • 8,202
  • 2
  • 40
  • 59
  • 2
    Code dumps don't make for great answers. Even if copy & pasting this happens to work for the user, it's doubtful that anyone has ever learned a whole lot from copy & pasting. – nhgrif Mar 27 '16 at 13:26
  • so what you mean is it should be explained in addition to the code written? by the way copy pasting was done so it would be convinent for him to use – hariszaman Mar 27 '16 at 13:54
  • It's fine for the copy & pastable code snippet to be part of your answer, but if the answer is nothing outside that snippet, it doesn't have a ton of value. – nhgrif Mar 27 '16 at 13:59
  • sorry, I am just trying to learn what do you mean by "nothing outside that snippet, it doesn't have a ton of value."? – hariszaman Mar 27 '16 at 20:25
  • You need to add an explanation of what this code is doing. Although, here, it doesn't really matter. This question has been closed as a duplicate of another. If you go to the duplicate question, you can see good examples of what explanatory answers look like. – nhgrif Mar 27 '16 at 20:27
  • @nhgrif 1. You are right I will update the answer 2. the question you have linked do not have the appraoch of sharing data backwards with delegate – hariszaman Mar 27 '16 at 21:16