简体   繁体   English

Swift 3.0在视图控制器之间传递数据

[英]Swift 3.0 Passing data among view controller

I'm using Xcode Swift 3.0, I have 5 view controllers, each controller have several textField. 我正在使用Xcode Swift 3.0,我有5个视图控制器,每个控制器都有几个textField。 Once the information finish filled, press nextButton to next view controller. 信息完成填写后,按nextButton到下一个视图控制器。 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..... 例如,在视图控制器1中完成所有textField的填充,单击nextButton然后我将它转换为查看控制器2,依此类推......

I want to get view controller 1,2,3,4 entered textField information only from view controller 5. 我想让视图控制器1,2,3,4仅从视图控制器5输入textField信息。

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? 如果不使用Segue将数据从vc1传递到vc2,从vc2传递到vc3等,我将如何做到这一点?

Please advice. 请指教。

TempData can be used for passing value from Controller to View and also from Controller to Controller. TempData可用于将值从Controller传递到View以及从Controller传递到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();
    }
}

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? 如果不使用Segue将数据从vc1传递到vc2,从vc2传递到vc3等,我将如何做到这一点?

No matter whether you're using Swift or Objective-C, the right solution here is to use a data model. 无论您使用的是Swift还是Objective-C,这里的正确解决方案都是使用数据模型。 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. 由于其他视图控制器也引用相同的模型,因此它们都会自动获取用户的更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM