0

我试图弄清楚如何保存 UIScrollView 的状态。例如,我在从 xib 加载的 UIScrollView 中有几个图像和标签。

现在我的问题是,当用户返回此页面时,我如何才能使滚动视图的状态与他们停止滚动时的状态相同?有谁知道如何做到这一点?谢谢。

我发现了这个:

NSUserDefaults  *defaults = [NSUserDefaults standardDefaults];
[defaults setFloat: scrollview.contentOffset.x forKey: @"myScrollPositionX"];
[defaults setFloat: scrollview.contentOffset.y forKey: @"myScrollPositionY"];

和恢复:

scrollView.contentOffset = CGPointMake([defaults floatForKey: @"myScrollPositionX"], [defaults floatForKey: @"myScrollPositionY"]); 

我应该在哪里实现这个?对不起,我是新人,所以请简单点:D

4

1 回答 1

1

You can save the position to the defaults in the viewWillDisappear:animated method of your view controller, and set the content offset in the viewWillAppear:animated method, so the state is saved as the user leaves the view, and is restored just before the user re-enters the view.

于 2012-10-13T20:59:00.000 回答