-1

I want to pass data between two view controllers, but don't want the view to change when the users presses my save data button.

The users needs to fill in multiple data fields, and when finish can press another button to go to the second view controller.

I found many tutorials how to pass data using segue, but they all change view as soon as the 'save button is pressed'.

Any one can explain to me how to alter the code?

@Phillip Mills: here is how I used your code. (what am I doing wrong?)

code: //////// declaring classes on FirstViewController (trying it first on only one ViewController)

class FakeVC1 {
    func userInput() {
        DataModel.shared.username = outbj14u.text
}

class FakeVC2 {
    func viewAppears() {
        if let name = DataModel.shared.username {
            outbj14p.text = name
            print("I have nothing to say")
        }
    }
}

class DataModel {
    static let shared = DataModel()
    var username: String?
}



////till here

//// here is where i call the functions

override func viewDidAppear(_ animated: Bool) {

    FakeVC1().userInput()
    FakeVC2().viewAppears()

    if let xbj14p = UserDefaults.standard.object(forKey: "outbj14p") as? String
    {
        outbj14p.text = xbj14p
    }






    if let xbj14u = UserDefaults.standard.object(forKey: "outbj14u") as? String
    {
        outbj14u.text = xbj14u
    }

////

@Phillip Mills: Below is what I have know. I think I got the code on the FirstViewController right, but the code on the Second View controller must be wrong. I don't get any errors, but the text field on the SecondViewController remains unchanged after putting input on in the FirstViewController

//// Code on the FirstViewController

class DataModel {
    static let shared = DataModel()
    var username: String?
}


@IBAction func savebj14p(_ sender: Any) {
    outbj14p.text = inbj14p.text
    DataModel.shared.username = outbj14p.text
    UserDefaults.standard.set(inbj14p.text, forKey: "namebj14p")
}

//and on the SecondViewController

@IBOutlet weak var bj14u: UILabel! // connected to a label

//and

class DataModel {
    static let shared = DataModel()
    var username: String?
}


override func viewDidAppear(_ animated: Bool) {


    if let name = DataModel.shared.username {
        bj14u.text = name
    }

}
Andre
  • 3
  • 2

3 Answers3

1

In your case, don't pass data.

Create a shared object to act as your data model. When users fill in the fields, update the data model.

When the user moves to the second controller/view, that controller uses the data model object to show what it needs to.

class FakeVC1 {
    func userInput() {
        DataModel.shared.username = "Me"
    }
}

class FakeVC2 {
    func viewAppears() {
        if let name = DataModel.shared.username {
            print(name)
        } else {
            print("I have nothing to say")
        }
    }
}

class DataModel {
    static let shared = DataModel()
    var username: String?
}

FakeVC1().userInput()
FakeVC2().viewAppears()
Phillip Mills
  • 30,888
  • 4
  • 42
  • 57
  • Great, thanks for your advice. Any tutorial you can recommend for creating a shared object? and how to use it as a data model? I couldn't find one. – Andre Oct 17 '17 at 13:32
  • This looks great, will try it in my code later today. Will give you an update how it went :) Thanks again – Andre Oct 17 '17 at 14:09
  • Here's a quick way to create a singleton. It may or may not be appropriate for your use case, so don't go crazy with them. https://krakendev.io/blog/the-right-way-to-write-a-singleton – Adrian Oct 17 '17 at 14:30
  • Hi, I tried but I must misunderstand. My variable assigned to the user's input is inbj14u my label that outputs is outbj14u. (so far all the code on the first view-controller for testing. Pleas see my code above in the edited post – Andre Oct 18 '17 at 18:38
  • Don't use the fake view controller classes in your code. I included those to show you how real view controllers should write to, and read from, the data model class. Where I have `userInput`, you should be saving the real input and, where I have `viewAppears `, you should be updating your actual views. – Phillip Mills Oct 18 '17 at 19:10
  • Hi, I get no errors, but when I run the app, I don't get the value to appear on the SecondViewController. Please see my second edit on the post for the code I got now. – Andre Oct 18 '17 at 21:48
  • Try putting `DataModel` into its own source file instead of declaring it twice. – Phillip Mills Oct 19 '17 at 12:58
  • Great I got it (I have it in its' own swift file in the same project. Works like a charm. Thank you for your help :) – Andre Oct 20 '17 at 22:28
0

If you need to pass value to another viewcontroller without changing the view , you can user NSNotificationCenter class

Refer this link for more details

NSNotificationCenter addObserver in Swift

DHEERAJ
  • 1,478
  • 12
  • 32
0

what i will recommend is to use a global variable or array, you will have the info in all view controllers and you will be able to call it in your new view controller.