Xcode:故事板并在每个控制器中保留数据
正如你可以猜到的,我是一名新程序员,但我很难得到一个简单的东西! 我正在制作一个带有多个视图控制器的应用程序。每个控制器都有文本字段和 UIsegmentedControl 项。当我从一个视图控制器移动到另一个视图控制器时(如果重要的话,使用模态转换),前一个视图控制器的内容(文本字段条目和分段控制选项)将重置为其原始状态。我怎样才能让他们保持以前的状态? 提前致谢。
As you can guess i am a new programmer and i have trouble getting a simple thing!
I am making an app with multiple view controllers. Each controller have textfields and UIsegmentedControl items. When i am moving from one view controller to the other (uding modal trantition if that matters), the contents of the previous one (textfield entries and segmented control option) reset to their original state. How can i make them keep their previous state?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我希望这段代码能够解决您的问题。它保存其中任何内容的标签。并且您需要向其他类添加更多代码。
如果这段代码对您有帮助,请告诉我,我可以给您完整的代码。
This piece of code will solve your problem, I hope. It saves the label of whatever is inside of it. And you need to add some more code to other classes.
If this code helps you tell me and I can give you the whole code.
要保存应用程序状态,您可以使用模型类,遵循推荐的 MVC(模型-视图-控制器)范例。
更多信息请参见:重新加载时保留视图状态
作为替代方案 您可以使用
viewWillDisappear:
事件保存视图状态,然后在viewWillAppear:
事件上恢复它。viewWillDisappear:
事件在视图即将消失之前触发,而viewWillAppear:
在视图置于前台之前触发,非常适合对视图进行任何更改用户界面。这些事件可能已经在您的视图控制器中为您声明了,但如果它们没有在此处检查原型:http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html
To save the application state you can use a model class, following the recommended MVC (model-view-controller) paradigm.
More information here: Retain view state upon reloading
As an alternative you could use the
viewWillDisappear:
event to save your view state, and then restore it on theviewWillAppear:
event.The
viewWillDisappear:
event is fired right before the view is going to disappear, andviewWillAppear:
is fired before the view is put on the foreground, being ideal to make any changes to the UI.These events might have already been declared for you in your view controller, but in case they're not check the prototypes here: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html
您还可以使用导航控制器从一个视图移动到另一个视图。
这样,您将把新视图推到前一个视图之上,并且当您返回时,前一个视图仍保持其状态。
有关 Storyboard 和 UINavigationController 的更多信息,请参阅本教程:
http://www.raywenderlich.com/5138/beginning -storyboards-in-ios-5-part-1
You can also use a navigation controller to move from one view to another.
This way, you will push your new view on top of the previous one, and when you go back, the previous view has kept its state.
see this tutorial for more information on storyboard and UINavigationController :
http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1