Reputation: 11295
I have a controller for a "settings view" in where the user changes some of the properties of what's drawn in my MyView view. But, I can't figure out how to call setNeedsDisplay on MyView instead of my SettingsView.
MyView is an UIView, and it has its own controller called MyViewController.
Inside this view, I'm drawing rectangles of a kind, and with different colors. In SettingsViewController I change a parameter which affects what kind of rectangles are drawn in MyView. However, for that to happen, the user has to (they're on different tabs) switch views, and perform some action so that setNeedsDisplay gets called. I'm trying to call setNeedsDisplay FROM SettingsViewController, but I can't do self.view setNeedsDisplay beacause it'll call setNeedsDisplay on MyView. Does this make more sense?
Upvotes: 0
Views: 260
Reputation: 41226
A better question to ask would be "How can I design this so that my view controllers are self-contained and not dependent on the hierarchy of other view controllers?"
It seems like the best solution in your case might be to use notifications to let your drawing view controller know that it needs to refresh because settings have changed.
In your settings view controller.h add:
#define kSettingsChangedNotification @"SettingsChangedNotification"
In your Settings view controller, when settings have been updated you can use:
[[NSNotificationCenter defaultCenter] postNotificationName:kSettingsChangedNotification
object:self
userInfo:nil];
And then in your drawing view controller add the following to your viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(onSettingsChanged:)
name:kSettingsChangedNotification
object:nil];
And, finally define onSettingsChanged in your drawing view controller:
-(void)onSettingsChanged:(NSNotification*)notification
{
[self.view setNeedsDisplay];
}
Upvotes: 2
Reputation: 1514
You just need to call setNeedsDisplay
on the view that you want to redraw. So if you're in SettingsViewController
you need a handle to MyViewController
. You mentioned you're in a TabBarController
, so you could access the view through this. Eg (assuming MyViewController is the first tab and you're in SettingsViewController in another tab):
[[self.tabBarController.viewControllers[0] view] setNeedsDisplay]
Upvotes: 2