views:

285

answers:

2

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];
}

}
A: 

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.

Dietrich Epp
+2  A: 

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);
}
Rob Keniger
So to summarize, In order to update the genCount, I don't need to implement an NSNotification object because I can use a custom setter method to accomplish redrawing of the display.
samfu_1
That's correct. The main change from your implementation is that is is the view *controller*, not the *view*, that makes the decision on what the view should display. The view just changes its state based on a flag. Since the view controller always has a reference to the view using `self.view` then there's no need to use a notification, you just set the property on the view directly. Notifications are normally used when you need one or more objects to react to a change in another object when there is no direct connection, through ivars, outlets or otherwise.
Rob Keniger