1

I'm using Xcode Swift 3.0, I have 5 view controllers, each controller have several textField. Once the information finish filled, press nextButton to next view controller. For example, finish fill in all textField in view controller 1, click nextButton and I will segue it to view controller 2, and so on.....

I want to get view controller 1,2,3,4 entered textField information only from view controller 5.

How am I gonna to do that without using Segue to pass data from vc1 to vc2, and from vc2 to vc3 and so on?

Please advice.

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
Yuan
  • 107
  • 2
  • 13
  • 1
    Possible duplicate of [How do you share data between view controllers and other objects in Swift?](http://stackoverflow.com/questions/29734954/how-do-you-share-data-between-view-controllers-and-other-objects-in-swift) – vadian Mar 31 '17 at 15:41
  • vc1 finish and segue to vc2, vc2 finish and segue to vc3, vc3 finish and segue to vc4, vc4 finish and segue to vc5, At vc5, I want to gather all information of vc1,vc2,vc3,vc4. Any better approach other than segue? – Yuan Mar 31 '17 at 15:55

2 Answers2

0

TempData can be used for passing value from Controller to View and also from Controller to Controller. Find the example:

public class FirstController : Controller
{
    // GET: First
    public ActionResult Index()
    {
        TempData["Message"] = "Hello MVC!";
        return new RedirectResult(@"~\Second\");
    }
}
public class SecondController : Controller
{
    // GET: Second
    public ActionResult Index()
    {
        return View();
    }
}
Idan
  • 5,405
  • 7
  • 35
  • 52
Vinutha N
  • 156
  • 6
  • Hi, why can't I make something like this in VC2? let vc1 = viewcontroller1() and then vc1.myVariableInVC1?.text – Yuan Apr 01 '17 at 00:16
0

How am I gonna to do that without using Segue to pass data from vc1 to vc2, and from vc2 to vc3 and so on?

No matter whether you're using Swift or Objective-C, the right solution here is to use a data model. Each view controller gets a reference to a common data model from the object that creates it, and the view controller uses the model to populate its views. The view controller updates the model as the user makes changes. Since the other view controllers also refer to the same model, they all automatically get the user's changes.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • Okay, I will search over the net for the data model & core data, I would like to try it out by myself first, If I couldn't get it, I will ask from you again, is it okay? – Yuan Apr 01 '17 at 00:31
  • You don't need Core Data -- a data model is just an object that manages the internal state of your app. Read up on MVC rather than Core Data. – Caleb Apr 01 '17 at 00:58
  • Hi Caleb, I've tried your method by putting all my information to common data model. However, everytime I call this data model from other VC, it always return nil. `code` this is my data model code: import Foundation class Model : NSObject{ var cusAddress : String? } this is my UIviewcontroller code : var toModel = Model() @IBAction func nextButton(_ sender: UIButton) { if customerContact.text != ""{ performSegue(withIdentifier: "OD", sender: sender) toModel.cusAddress = customerAddress.text } } – Yuan Apr 01 '17 at 10:14
  • @Yuan It's next to impossible to read code in comments, but I'll point out that all your view controllers need to share *the same instance* of the model. If each one creates its own instance of the model, then each model will have its own data, right? And the whole point here is to share your data between the various controllers. – Caleb Apr 01 '17 at 16:32
  • Hi Caleb, i really really appreciate your time here. You see, I've create an model, inside that model consist of all the var of all 5 viewcontrollers. However, when i segue from vc1 to vc2, i intent to retrieve the saved data in vc2, but it return just nil. I think i missed something in between. – Yuan Apr 02 '17 at 00:46
  • @Yuan Sorry, I have no way to know what the problem might be without seeing the code, and it sounds like it should really be a separate question anyway. However, if your code in "vc2" is like what you posted in your previous comment, the problem may be that you're looking at a new, separate instance of the model rather than sharing the same model object between controllers. That certainly would explain the symptom. – Caleb Apr 02 '17 at 04:58
  • how am i gonna to share the model? Please guide me or give me some website to read. sorry, im very new to swift. – Yuan Apr 02 '17 at 11:09
  • This really has nothing to do with Swift -- it'd be exactly the same in Objective-C. The object that creates a view controller should provide the model. Maybe the app delegate creates the model and also the first view controller, so it should provide that model to that view controller. If that view controller then creates another view controller, it should provide the model to it at that point. – Caleb Apr 02 '17 at 16:14
  • my vc sequence is like this, starting vc1 -> vc2 -> vc3 -> vc4 -> vc5. I'm embedded all of the vcs' into navigation controller. I know theres way to pass thing using segue, however, I'm looking for more versatile way, like central data collection of all the vcs'. Is it by creating a swift model ? If yes, I couldn't even extract the data which inserted into vc1 earlier on, from vc2 or vc3, vc4, vc5. Am I mis-concept? – Yuan Apr 05 '17 at 02:10
  • You should use a dependency injection approach to share the model from one view controller to the next, and the natural way to do that with storyboards is to use `prepare(for segue: sender:)`. This is more versatile, not less, because the alternative is to make the model a globally shared object, which will make your code *less* flexible, not more so. The model consolidates all the state that the various controllers might need into one place, so that it's easy to pass around, and to put all the logic related to that state in one place separate from the controllers and views. Try it. – Caleb Apr 05 '17 at 03:58