简体   繁体   English

如何将数据从第三视图控制器传递回第一视图控制器?

[英]How Do I Pass Data from Third View Controller Back to First View Controller?

I currently have a navigation controller that contains one view controller and two table view controllers. 我目前有一个导航控制器,其中包含一个视图控制器和两个表视图控制器。 When the user navigates to the second table view controller, they can press an '+' bar button item which modally presents another table view with a static cell that contains a text field. 当用户导航到第二个表格视图控制器时,他们可以按下“ +”栏按钮项目,该项目以模态显示带有包含文本字段的静态单元格的另一个表格视图。 The user can enter text and press 'Done' to save it. 用户可以输入文本,然后按“完成”进行保存。 When my delegate method is called, the view controller is dismissed and that user-entered string is appended to the array in the second view controller. 当我的委托方法被调用时,视图控制器被关闭,并且用户输入的字符串被附加到第二个视图控制器中的数组。 The table view then displays that user-entered text in a table cell. 然后,表格视图在表格单元格中显示用户输入的文本。 The array has been passed up the stack via the prepareForSegue method. 该数组已通过prepareForSegue方法传递到堆栈中。 This is currently working because I can see the strings in the table view that the array was instantiated with in the first view controller. 当前这是可行的,因为我可以在表视图中看到在第一个视图控制器中实例化了数组的字符串。

My problem occurs when the user navigates back down the stack to any view controller prior to the second table view, which contains the user-entered strings displayed in cells. 当用户在第二个表视图之前向下浏览堆栈到任何视图控制器时,会出现我的问题,该视图包含在单元格中显示的用户输入的字符串。 I have successfully been able to pass the data to the previous view controller, obviously, but after much research and testing, I am unable to successfully get the user-entered string to persist. 很显然,我已经成功地将数据传递给了以前的视图控制器,但是经过大量的研究和测试,我无法成功地保存用户输入的字符串。 When I navigate to previous view controllers and then back go the second table view, the user-entered data is gone, and the array contains only the initial data. 当我导航到先前的视图控制器,然后返回第二个表视图时,用户输入的数据消失了,并且数组仅包含初始数据。

I understand that arrays in Swift are structures and the values are copied when passed up the stack. 我知道Swift中的数组是结构,并且在向上传递栈时会复制值。 This makes sense as to why the user-entered data disappears when I navigate to a previous view controller. 这对于为什么当我导航到上一个视图控制器时为什么用户输入的数据会消失是有道理的。 The previous view controller does not actually contain that user-entered data. 先前的视图控制器实际上并不包含该用户输入的数据。 Does this mean that I need to append the string to the array in the initial view controller instead of the array in the second table view controller? 这是否意味着我需要将字符串附加到初始视图控制器中的数组,而不是第二个表视图控制器中的数组? If that is the case, I am unsure of how to access the initial view controller from the third view controller. 如果是这种情况,我不确定如何从第三个视图控制器访问初始视图控制器。

All of the documents and tutorials that I have accessed are based on passing the data from the current view controller back to the previous view controller, which is the delegate that then dismisses the view controller that accepted the input. 我已访问的所有文档和教程都是基于将数据从当前视图控制器传递回先前的视图控制器的,该视图控制器是委托,然后将接受输入的视图控制器解散。

How can I pass the user-entered string from the last view controller back to the initial view controller? 如何将用户输入的字符串从最后一个视图控制器传递回初始视图控制器?

Actually, there are a lot of messaging mechanisms in Objective C, and in addition to Phillip's answer I can suggest you to use NSNotificationCenter by using which your can post notifications and catch them where needed. 实际上,Objective C中有很多消息传递机制,除了Phillip的回答外,我建议您使用NSNotificationCenter ,通过它您可以发布通知并在需要时捕获它们。 Posted notification can have custom object, string in your case, and name, for which you should add observer in order to receive that notification. 发布的通知可以具有自定义对象,您的情况下的字符串和名称,您应该为其添加观察者以接收该通知。

Registering for particular notification: 注册特定通知:

[[NSNotificationCenter defaultCenter] addObserver:{object_to_handle_notification} selector:@selector(notificationReceived:) name:@"my_notification" object:nil];

Posting notification: 发布通知:

[[NSNotificationCenter defaultCenter] postNotificationName:@"my_notification" object:userEnteredString];

Handling notification: 处理通知:

- (void)notificationReceived:(NSNotification *)notification
{
    NSString *userEnteredString = notification.object;
    //do whatever you want with the user entered string
}

Also, you should remove observer for the notification when you are done. 另外,完成后,您应该删除通知的观察者。 Hope this was helpful. 希望这会有所帮助。 Good Luck! 祝好运!

Create a class that is responsible for maintaining your data model. 创建一个负责维护数据模型的类。 You can either make it a singleton or have an object of the class available through your application delegate (or some other option). 您可以将其设置为单例,也可以通过应用程序委托(或某些其他选项)使该类的对象可用。 When a controller wants information to be available to other parts of the app, it sends it to the data model for update. 当控制器希望信息可用于应用程序的其他部分时,它将其发送到数据模型以进行更新。 When a controller wants to display information, it gets it from the data model. 当控制器想要显示信息时,它会从数据模型中获取信息。

No more data-dependencies between controllers that have to figure out what to exchange. 无需弄清楚交换什么内容的控制器之间就不再需要数据依赖。

What you can do is pass the firstViewController as a parameter to the ThirdViewController in the prepareForSegue method from the SecondViewController. 您可以做的是将firstViewController作为参数从SecondViewController传递给prepareForSegue方法中的ThirdViewController。

FirstViewController: FirstViewController:

class FirstViewController: UIViewController {
  var a:String!

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
      var destination=segue.destinationViewController as? SecondViewController
      destination?.firstViewCont=self
    }
}

SecondViewController: SecondViewController:

class SecondViewController: UIViewController {
  var firstViewCont:FirstViewController!

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
      var destination=segue.destinationViewController as? ThirdViewController
      destination?.FViewCont=self.firstViewCont
    }
}

ThirdViewController: ThirdViewController:

class ThirdViewController: UIViewController {
  var FViewCont:FirstViewController!

  @IBAction func textFieldEdited(sender: UITextField) {
         FViewCont.a!=sender.text
  }
}

This is one of the most impractical ways one would do it. 这是一种最不切实际的方法。 I suggest that you learn about Navigation View Controllers . 我建议您了解导航视图控制器 They work like a charm when doing something like this. 当做这样的事情时,它们就像一种魅力。

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

相关问题 如何从模态视图控制器将数据传递回视图控件 - How to pass data back to view control from modal view controller 如何将数据传回Swift中的第一个视图控制器? - How to pass data back to the first view controller in Swift? 如何使用 segue 将数据从 First ViewController 直接传递到 Third View Controller? - How to pass data from First ViewController directly to the Third View Controller using segue? 如何将数据从异步任务传递到视图控制器? - How do I pass data from an asynchronous task to a view controller? 如何将数据传递回堆栈中的视图控制器? - How Pass Data back a view controller in the stack? 关闭时如何将数据从模态视图控制器传回 - How to pass data from modal view controller back when dismissed 如何从未连接到其他视图控制器的视图控制器传输回视图控制器? - How do I transfer back to a view controller from a view controller that is not connected to other view controllers? 如何将数据从View Controller传递到Container View? - How would I pass data from a View Controller to a Container View? 如何将信息从一个视图控制器中的表视图单元传递到另一视图控制器? - How do I pass information from a table view cell in one view controller to another view controller? 如何将视图 controller 中的数据传递到表视图 controller? - How to pass the data from view controller to the table view controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM