NSNotification-其他类事件的实现?

发布于 2024-09-12 14:28:29 字数 931 浏览 8 评论 0原文

嗨,

在我的 ViewController.mi 中,在“viewDidLoad”中添加了一个 NSNotification,如下所示:

        [[NSNotificationCenter defaultCenter] addObserver:self
                                    selector:@selector(pageControlChanged:) 
                                    notificationName:@"ScrollViewDidEnd"
   object:nil];

然后我有一个自定义滚动视图类“MyScrollView”,我可以在其中滚动图像。当调用“scrollViewDidEndDecelerating:(UIScrollView *)scrollView{..”方法时,我在那里添加了一个 postNotification。

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
[[NSNotificationCenter defaultCenter] postNotificationName:@"ScrollViewDidEnd" object:nil];
}
- (void) pageControlChanged:(NSNotification*)notification{
    NSLog(@"pagecontrol: %@", notification);

}

当我编译我的项目时,出现错误并且应用程序崩溃: 控制台输出:“未找到 addObserver:selector:notifcatonName:object:”方法。

所以,这是我第一次使用 NSNotification,如果能在这里获得一些帮助,那就太好了。 感谢您抽出时间。 哟什

HI,

in my ViewController.m i´ve added a NSNotification in "viewDidLoad" like this:

        [[NSNotificationCenter defaultCenter] addObserver:self
                                    selector:@selector(pageControlChanged:) 
                                    notificationName:@"ScrollViewDidEnd"
   object:nil];

Then i´ve a custom scrollView-class "MyScrollView" where i can scroll an image. I´ve added a postNotification there, when the "scrollViewDidEndDecelerating:(UIScrollView *)scrollView{.." method is called.

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
[[NSNotificationCenter defaultCenter] postNotificationName:@"ScrollViewDidEnd" object:nil];
}
- (void) pageControlChanged:(NSNotification*)notification{
    NSLog(@"pagecontrol: %@", notification);

}

When i compile my project, i get an error and the app crashes:
Console-output: "No addObserver:selector:notifcatonName:object:" method found.

So, that´s my first NSNotification usage, would be great to get some help here.
Thanks for your time.
yosh

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

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

发布评论

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

评论(1

农村范ル 2024-09-19 14:28:29

您正在寻找的方法是:(

- (void)addObserver:(id)notificationObserver selector:(SEL)notificationSelector name:(NSString *)notificationName object:(id)notificationSender

注意 name:,而不是 notificationName:

所以您的代码应该是:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(pageControlChanged:)
                                             name:@"ScrollViewDidEnd"
                                           object:nil];

The method you're looking for is:

- (void)addObserver:(id)notificationObserver selector:(SEL)notificationSelector name:(NSString *)notificationName object:(id)notificationSender

(note the name:, not notificationName:)

So your code should be:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(pageControlChanged:)
                                             name:@"ScrollViewDidEnd"
                                           object:nil];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文