简体   繁体   English

在视图之间导航时保​​留UI数据

[英]Keep UI data when navigating between views

In my test project I have a ViewController and a TableViewController controller embedded in a Navigation Controller. 在我的测试项目中,我在导航控制器中嵌入了一个ViewController和一个TableViewController控制器。 The ViewController is the main view, and the user can navigate to the TableViewController and then return back to the ViewController. ViewController是主视图,用户可以导航到TableViewController,然后返回到ViewController。

I am using a 'push' segue when going from ViewContoller>TableViewController, and the TableViewController is dismissed using [self dismissViewControllerAnimated:YES completion:nil]; 当我从ViewContoller> TableViewController进入时,我正在使用'push'命令,并且使用[self dismissViewControllerAnimated:YES completion:nil];来关闭TableViewController [self dismissViewControllerAnimated:YES completion:nil]; when the user wishes to go back to ViewController. 当用户希望返回ViewController时。

In ViewController, I have a button that changes the text on a label: 在ViewController中,我有一个按钮可以更改标签上的文本:

-(IBAction)onButtonPress:(id)sender {
      _myLabel.text = @"New Label Text";
}

When navigating to TableView, and then back to ViewController, the change in the _myLabel.text has been lost and the original text is restored . 导航到TableView之后再返回到ViewController时, _myLabel.text中的更改已丢失,并且原始文本已恢复 What is the best way to ensure UI data is retained when navigating between views? 在视图之间导航时,确保保留UI数据的最佳方法是什么? I might only have one label in this project, but at some point I will have many UI elements, for example, WebViews that need to keep a page loaded when the user navigates away and then comes back. 在这个项目中,我可能只有一个标签,但是到某个时候,我将有很多UI元素,例如,WebView,当用户离开然后返回时,需要保持页面加载。

What would you suggest is the best method to implementing this? 您认为实现此目标的最佳方法是什么?

Thanks for your time. 谢谢你的时间。

First of all, View is your data representation, any data is your Model. 首先,视图是您的数据表示形式,任何数据都是您的模型。 So you may store Model with its entities and values. 因此,您可以将模型及其实体和值存储起来。 Your controllers manage data to represent it on View or to change Model according to user actions in View. 您的控制器管理数据以在View上表示数据或根据View中的用户操作更改模型。

So add some Model-object for storing _myLabel.text . 因此,添加一些用于存储_myLabel.text模型对象。 And use this object for both controllers in your app. 并将此对象用于应用程序中的两个控制器。

尝试从ViewWillAppearViewDidAppear方法中删除设置文本,将其放在ViewDidLoad

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

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