0

Im new in swift. I have create a struct class with one variable. Im trying to set value of that struct variable from my first view controller which is working perfectly but when im trying to get that same value from second view controller its give me nil value.

below is my some codding

//Struct class

import Foundation
import UIKit

struct CompanyData {
    var companyName : String?


    mutating func setData(name : String)
    {
        companyName = name
    }

}

// FIRST VIEW CONTROLLER

import UIKit

class SelfCompanyNameView: UIViewController {

@IBOutlet weak var companyNameTxt: TextfieldDesign!
var company = CompanyData()
var comName = ""

override func viewDidLoad() {
    super.viewDidLoad()

}


@IBAction func compnyBtnPress(_ sender: Any)
{
    if companyNameTxt.text?.count == 0
    {
        Alert.showAlert(on: self, with: "Required", message: "Please enter your company name")
    }
    else
    {
        comName = companyNameTxt.text!
        company.setData(name: comName)
        print("\(comName)===\(company.companyName!)")


        let vc = storyboard?.instantiateViewController(withIdentifier: "SelfAddressView")  as! SelfAddressView
        navigationController?.pushViewController(vc, animated: true)
    }
   }
 }

//SECOND VIEW CONTROLLER

import UIKit

class SelfAddressView: UIViewController {

var company = CompanyData()
override func viewDidLoad() {
    super.viewDidLoad()

    print(company.companyName)
 }
}

3 Answers 3

1

You need to pass your model to secondView Controller like following code

// FIRST VIEW CONTROLLER

let vc = storyboard?.instantiateViewController(withIdentifier: "SelfAddressView")  as! SelfAddressView
vc.company = company
navigationController?.pushViewController(vc, animated: true)

//SECOND VIEW CONTROLLER

import UIKit

class SelfAddressView: UIViewController {

var company: CompanyData!
override func viewDidLoad() {
    super.viewDidLoad()

    print(company.companyName)
 }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You need to declare a static variable as a shared instance of your struct. And use that while setting and getting your value. E.G.

struct CompanyData {
    static let shared = CompanyData()
    var companyName : String?

    mutating func setData(name : String)
    {
        companyName = name
    }

}

While setting the value, use as:

company.shared.setData(name: comName)

And while getting the value, use as:

print(company.shared.companyName)

Comments

0

I would rather use Protocols and delegators. Here is a great tutorial will help you understand the concept Sean Allen Swift Delegate Protocol Pattern Tutorial - iOS Communication Patterns Part 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.