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