NSNotification 是检测变化的正确选择吗?

发布于 2024-08-18 13:37:30 字数 838 浏览 5 评论 0原文

我有一个 UIView 的自定义子类,当值 (NSInteger) 低于某个值时,它需要执行选择器。从文档中可以看出,我需要设置一个观察者对象来查找此更改。

NSInteger 值存在于视图控制器中,并且 UIView 子类作为同一视图控制器中的子视图实现。

我想知道像下面这样的事情是否在正确的轨道上:

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

我正在努力思考在哪里添加该观察者执行操作的条件。例如,如果条件是:

if(genCount < 3) {
///code statement
}

我希望我的观察者查找上述更改,然后执行后续操作。我会像这样将其添加到我的通知对象中吗?

- (void)genCountLow:(NSNotification *)notification {
    if (genCount < 3) {
[electricalSystemDiagramView depowerShedBuses];
}

}

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲鸠 2024-08-25 13:37:30

你的视图不应该做出这个决定,视图控制器应该。通常,您应该尝试避免视图需要有关模型或控制器实现的信息的情况。

您的视图应该有一个标志,例如名为 drawLowStateBOOL 属性。当此值更改时,应通过执行以下操作以不同的外观重新绘制视图:

YourView.m:中,

- (void)setDrawLowState:(BOOL)isLow
{
    if(drawLowState != isLow)
    {
        drawLowState = isLow;
        [self setNeedsDisplay];
    }
}

您可以通过更改在视图对象的绘制内容来更改视图的外观基于drawLowState属性值的绘制例程。

由于您的视图控制器知道整数值何时更改,因此您可以轻松地在整数属性的设置器中做出决定,并告诉视图更新其外观:

YourViewController.m:

- (void)setGenCount:(NSInteger)aCount
{
    genCount = aCount;
    self.view.drawLowState = (genCount < 3);
}

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);
}
叹梦 2024-08-25 13:37:30

我会将用于发布通知的代码放在视图类中。例如:

- (void)setFrobs:(NSInteger)frobs {
    if (frobs < 3 && _frobs >= 3)
        [[NSNotificationCenter default...] postNotificationName:...];
    _frobs = frobs;
}

然后,当您更改 frobs 的值时,请始终使用 setFrobs。请注意,我对您的视图类一无所知。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文