Search Your Question

How many ways to pass data from one view controller to another view controller?

Ans :

Through following methods, we can pass data from one viewcontroller to another viewcontroller.

1. Using segue (PrepareForSegue) method,
2. Delegate
3. Setting variable

Let's assume we have 2 viewcontroller named VC1 and VC2. We want to pass data from VC1 to VC2 and some times VC2 to VC1. For both cases, we can use segue to pass data. But in general practise, to pass data from VC1 to VC2 (Forward data passing), we use segue and from VC2 to VC1(Backward data passing), we use protocol - delegate way.

1. Using segue (PrepareForSegue) steps :  VC1 --->  VC2


  •   Draw segue from VC1 to VC2 and give identifier string 'InputVCToDisplayVC'  to segue.
  •   Take variable in VC2.swift as :

           var fullName : String?

  •    In VC1, write following code in PrepareForSegue method,


       override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if(segue.identifier == "InputVCToDisplayVC"){
let displayVC = segue.destination as! DisplayViewController
displayVC.fullName = nameTextField.text
        }
        }

  •       In VC2, viewDidLoad, we can get  value of fullName and access it.
2. Using delegate steps :    VC2  --->  VC1


Import UIKit

class VC1: UIViewController, VC2Delegate {


       override func viewDidLoad() {
                super.viewDidLoad()
                
       }

       func doSomething(data : String) {
                print(data)
      }


      override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      if(segue.identifier == "InputVCToDisplayVC"){
      let displayVC = segue.destination as! DisplayViewController
      displayVC.delegate = self
      }
       }

 }


Import UIKit
protocol VC2Delegate {
      func doSomething(data : String) 
}

class VC2 : UIViewController {
        
      var fullName : String?
      weak var delegate : VC2Delegate?
      
      override func viewDidLoad() {
             
     }

     @IBAction func btnPassToVC1_TouchUpInside(_ sender : UIButton) {
              if let delegate = delegate {
                     delegate.doSomething(data: btn.titleLabel.text)       
              }
    }
}


In above, method we make custom protocol and create its object as delegate.We set delegate reference to VC1. So on VC2 button pressed event, VC1 doSomething method will be called.

3. Setting Variable steps :     VC1   --->  VC2

  • Take variable in VC2.swift as :  
           var fullName : String?

  • On buttonPressed action of VC1, write code as :
           let vc2 : VC2 =  UIStoryboard(name: "Main", bundle:                                                                                                        nil).instantiateViewController(withIdentifier: "VC2_ID") as! VC2
           vc2.fullName = "iOS iQA"
           self.present(vc2, animated: true, completion: nil)

Note : There are also persistent storage, by which we can store data and access that data any where in our project. But this is not correct answer or solution for above question or problem. We can store data in NSUserDefault, PList, Coredata, SQLite, KeyChain Access, File as persistent storage.






    

No comments:

Post a Comment

Thanks