Xcode : Storyboard and retaining data in each cont

2019-06-23 01:19发布

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.

3条回答
We Are One
2楼-- · 2019-06-23 01:26

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楼-- · 2019-06-23 01:36

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

查看更多
Bombasti
4楼-- · 2019-06-23 01:42
-(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.

查看更多
登录 后发表回答