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);

}

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

所以,这是我第一次使用 NSNotification,如果能在这里得到一些帮助会很棒。谢谢你的时间。哟西

4

1 回答 1

1

您正在寻找的方法是:

- (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];
于 2010-08-02T09:38:15.080 回答