如何保存窗口状态

来源:互联网 发布:宽带网络分线盒 编辑:程序博客网 时间:2024/05/17 00:07
在iPhone手机客户端开发过程中经常会涉及到一些问题,如:如何保存窗口状态等等。

      当应用别切到后台后,很难保证应用不被杀掉(用户主动杀掉或被系统杀掉),如果被杀掉,当程序
再次启动时就需要恢复前一次状态。ios sdk 6.0提供了一些接口让我们很容易实现对应用状态的保存,具体做法如下:
      首先在AppDelegate中要实现如下几个方法:
  1. - (BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder {
  2. return YES;
  3. }
  4. - (BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder {
  5. return YES;
  6. }
  7. - (void)application:(UIApplication *)application willEncodeRestorableStateWithCoder:(NSCoder *)coder {
  8. [coder encodeObject:self.window.rootViewController forKey:PDAppDelegateRootViewCTLKey];
  9. }
  10. - (void)application:(UIApplication *)application didDecodeRestorableStateWithCoder:(NSCoder *)coder {
  11. UIViewController * ctl = [coder decodeObjectForKey:PDAppDelegateRootViewCTLKey];
  12. if (ctl) {
  13. UIWindow * window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  14. window.rootViewController = ctl; 
  15. self.window = window;
  16. }
  17. }
复制代码
程序启动时会先调用
shouldRestoreApplicationState方法,因为方法返回YES,表明恢复应用状态,这时会顺序调用
didDecodeRestorableStateWithCoder方法,最后调用
didFinishLaunchingWithOptions, 该方法实现如下:
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  2. if (_window == nil) {
  3. self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
  4. // Override point for customization after application launch.
  5. PDViewController *viewController = [[[PDViewController alloc] initWithNibName:nil bundle:nil] autorelease];
  6. self.window.rootViewController = viewController;
  7. }
  8. [self.window makeKeyAndVisible];
  9. return YES;
  10. }
复制代码
这里还涉及到一个ViewController的写法,该程序主要是为了保存ViewCtronller中的状态,所以ViewCtronller必须要支持状态恢复,如下:
  1. @interface PDViewController : UIViewController<UIViewControllerRestoration> {
  2. int _sliderValue;
  3. }

  4. @end
复制代码
为了支持状态恢复必须实现UIViewControllerRestoration接口,同时还有几个方法需要实现:
  1. - (void)encodeRestorableStateWithCoder:(NSCoder *)coder {
  2. [super encodeRestorableStateWithCoder:coder];
  3. [coder encodeInt:_sliderValue forKey:@"sliderValue"];
  4. }
  5. - (void)decodeRestorableStateWithCoder:(NSCoder *)coder {
  6. [super decodeRestorableStateWithCoder:coder];
  7. _sliderValue = [coder decodeIntForKey:@"sliderValue"];
  8. UISlider *slider = (UISlider*)[self.view viewWithTag:SliderTag];
  9. slider.value = _sliderValue;
  10. UITextField *textField = (UITextField*)[self.view viewWithTag:TextFieldTag];
  11. textField.text = [NSString stringWithFormat:@"%d", _sliderValue]; 
  12. }
  13. + (UIViewController *) viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder {
  14. UIViewController *retViewController = [[[PDViewController alloc] initWithNibName:nil bundle:nil] autorelease];
  15. return retViewController;
  16. }
复制代码
通过以上基本既可实现窗口状态的保存

参考:http://www.apkbus.com/android-130188-1-1.html
0 0