-1
  1. // here is code

    import UIKit
    
    class ViewController: UIViewController {
    
        @IBOutlet weak var txtNames: UITextField! 
        @IBOutlet weak var txtRollNumber: UITextField!
    
        override func viewDidLoad() {
            super.viewDidLoad()
           // Do any additional setup after loading the view. 
        }
    
        // button action
        @IBAction func actionNext(_ sender: UIButton) {
            let vc = storyboard?.instantiateViewController(withIdentifier:   
            "ViewController2") as! ViewController2
    
            demographics?.name = txtNames.text!
            demographics?.rollnumber = Int(txtRollNumber.text!)! 
           //        obj.name = txtNames.text! 
           //         obj.sirname = txtRollNumber.text!
           self.navigationController?.pushViewController(vc, animated: true)                
        }
    }
    

// code for 2 vc

class ViewController2: UIViewController { 
     @IBOutlet weak var txtFldname:UITextField! 
     @IBOutlet weak var txtFldRollnuber:UITextField!
       
     override func viewDidLoad() {
         super.viewDidLoad()
         txtFldname.text = demographics?.name
         txtFldRollnuber.text = "\(demographics?.rollnumber)"
   
         // Do any additional setup after loading the view. 
    }
       

// class for struct

import Foundation 
var demographics: names?
       
struct names { 
    var name = ""
    var rollnumber = Int()
}
vadian
  • 274,689
  • 30
  • 353
  • 361
  • 2
    You should probably properly format (which I'll send), and be more specific on what data you want transferred if you want someone to be able to help. – goatofanerd Aug 24 '21 at 04:36
  • As the controllers are directly related `Notification` is the worst choice. Use a callback closure – vadian Aug 24 '21 at 04:47
  • As @vadian said, a `Notification` is a bad choice. While it may work, it's not the best option. What I like to do is AFTER you do `self.navigationController?.pushViewController(vc, animated: true)`, I would say do `vc.textFldRollnuber.text = Int(txtRollNumber.text!)! ` and same for the `textFldname`. Not the most efficient or anything, but it gets the job done. – goatofanerd Aug 24 '21 at 05:02

1 Answers1

0

You can send and receive data using variables.


class ViewController: UIViewController {

    var passData: names?

    @IBOutlet weak var txtNames: UITextField! 
    @IBOutlet weak var txtRollNumber: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
       // Do any additional setup after loading the view. 
    }

    // button action
    @IBAction func actionNext(_ sender: UIButton) {
        let vc = storyboard?.instantiateViewController(withIdentifier:   
        "ViewController2") as! ViewController2

        passData?.name = txtNames.text!
        passData?.rollnumber = Int(txtRollNumber.text!)! 
        self.navigationController?.pushViewController(vc, animated: true) 
        vc.receiveData = passData  // pass data    
    }
}
class ViewController2: UIViewController { 

     @IBOutlet weak var txtFldname:UITextField! 
     @IBOutlet weak var txtFldRollnuber:UITextField!
     
     var receiveData: names?

     override func viewDidLoad() {
         super.viewDidLoad()
         if let receiveData = receiveData { 
          // If the data came in

           txtFldname.text = receiveData?.name
           txtFldRollnuber.text = "\(receiveData?.rollnumber)"
         }
    }
}
      
Hailey
  • 302
  • 1
  • 7