Reputation: 1160
I have a custom subclass of UIView that needs to perform a selector when a value (NSInteger) falls below a certain value. From what I can tell from the docs, I need to set up an observer object to look for this change.
The NSInteger value exists in the view controller and the UIView subclass is implemented as a subview in the same view controller.
I wondering if something like the following is on the right track:
-(void)createNotification:
[[NSNotificationCenter defaultCenter]
addObserver:self //since this is in the viewController, I'm thinking it's "self"
selector:@selector(genCountLow:)
name:@"ReviewGenCount"
object: nil ];
I'm struggling with where I would add the condition for this observer to perform the action. For instance, if the condition would be:
if(genCount < 3) {
///code statement
}
I want my observer to look for the above change and then perform a subsequent action. Would I add that to my notification object like this?
- (void)genCountLow:(NSNotification *)notification {
if (genCount < 3) {
[electricalSystemDiagramView depowerShedBuses];
}
}
Upvotes: 1
Views: 1262
Reputation: 46020
Your view should not be making this decision, the view controller should. Generally you should try and avoid the situation where the view needs to have information about the model or controller implementation.
Your view should instead have a flag, for example a BOOL
property named drawLowState
. When this value changes, the view should be redrawn with a different appearance by doing something like this:
In YourView.m:
- (void)setDrawLowState:(BOOL)isLow
{
if(drawLowState != isLow)
{
drawLowState = isLow;
[self setNeedsDisplay];
}
}
You'd change the appearance of the view by altering what you draw in the view object's drawing routine based on the value of the drawLowState
property.
Since your view controller knows when the integer value changes, you can then easily make the decision in the setter for your integer property and tell the view to update its appearance:
In YourViewController.m:
- (void)setGenCount:(NSInteger)aCount
{
genCount = aCount;
self.view.drawLowState = (genCount < 3);
}
Upvotes: 2
Reputation: 213448
I would put the code for posting the notification in the view class. Something such as:
- (void)setFrobs:(NSInteger)frobs {
if (frobs < 3 && _frobs >= 3)
[[NSNotificationCenter default...] postNotificationName:...];
_frobs = frobs;
}
Then, always use setFrobs when you change the value of frobs. Mind you, I don't know anything about your view class.
Upvotes: 0