开发者

iPhone updating view

I am trying to solve a problem considering update of an view. First I switch to another view by using:

- (void)viewSettings {
 settingsViewController = [[SettingsViewController alloc] initWithNibName:nil bundle:nil];
 [[self viewController] presentModalViewController:settingsViewController animated:YES];}

Which is a delegate Method called by

ivaskAppDelegate *mainDelegate = (ivaskAppDelegate *)[[UIApplication sharedApplication] delegate];
 [mainDelegate viewSettings];

I switch back by calling another dellegate method

- (void)settingsDone {
 [[self viewController] dismissModalViewControllerAnimated:YES];}

When I return to my view I now want to开发者_开发百科 update a label, can you explain how to do it?

I use NIB-files which have a controller class and a view class connected in the identity inspector.

/N


Although I heavily suggest delegation in this case there are two other options, that come to my mind: Notification and KVO.

Notification

Whenever settings are changed the settings view controller could post a Notification, to let other parts of the app know about this change. Posting a notification is easy as:

[[NSNotificationCenter defaultCenter]
      postNotificationName:@"SettingsChangedNotification" object:theNewSettings];

Every object that somehow want to know about a settings change can subscribe to that notification via:

//Subscribe (in viewDidLoad or init)
[[NSNotificationCenter defaultCenter]
          addObserver:self selector:@selector(settingsChanged:)
          name:@"SettingsChangedNotification" object:nil];

// Called when a "SettingsChangedNotification" is posted
- (void)settingsChanged:(NSNotification*)settingsChangedNotification {
    id newSettings = [settingsChangedNotification object];
}

//Unsubscribe (in viewDidUnload or dealloc)
[[NSNotificationCenter defaultCenter]
          removeObserver:self name:@"SettingsChangedNotification" object:nil];

See Notification Programming Topics

If you are trying to manages UserDefaults with your settingsViewController there's an even better way. Just set the values on the sharedUserDefaults and the app will post a NSUserDefaultsDidChangeNotification notification all on it's own. All objects that depend on user settings could subscribe to that notification, and after it's posted reread the userDefaults.

See

  • NSUserDefaults Class Reference
  • User Defaults Programming Topics

Key-Value Observing (KVO)

Your rootViewController could observe changes of an object, which it needs to synchronize with, by Key-Value Observing.

One object registers itself as observer for keyPaths on other objects by sending them a addObserver:forKeyPath:options:context: message. The object is informed about changes via the observeValueForKeyPath:ofObject:change:context: callback method. KVO could sometimes be difficult the get right, because you have to ensure, that you register and unregister the same number of times, before an object gets deallocated.

See Key-Value Observing Programming Guide

Conclusion

Please refrain from using "global" variables on your app-delegate. There's enough possibilities to do better. The sooner you dive into them, the better code you will write.


You will need to set up a delegate that is implemented in your main view controller (where you need to have the label updated), and that is called from your settings view controller. I have a blog post that describes how to do this:

http://www.dosomethinghere.com/2009/07/18/setting-up-a-delegate-in-the-iphone-sdk/


You would make your view controller conform to a protocol that your create. In this case it may be named SettingsDelegate. That protocol contains one message - (void)didFinishSettingsWithSomeNewValue:(id)newValue;

The Settings Controller has a delegate instance variable and a property of type id<SettingsDelegate>

Before you present the settings viewController you assign the parent view controller to the delegate property. In the settings view controller you send a didFinishSettingsWithSomeNewValue: message to your delegate (the parent view controller) along with some new value. The parent view controller implements that method and inside the implementation can dissmiss the modal controller and update any views on itself.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜