Xcode : Storyboard and retaining data in each controller

ε祈祈猫儿з 提交于 2019-12-13 13:08:58

问题


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.


回答1:


-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    bViewController *deneme = [segue destinationViewController];
    [deneme setPassedValue:label.text];
}

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.




回答2:


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 the viewWillAppear: event.

The viewWillDisappear: event is fired right before the view is going to disappear, and viewWillAppear: 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




回答3:


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



来源:https://stackoverflow.com/questions/8926116/xcode-storyboard-and-retaining-data-in-each-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!