NSNotificationCenter 在自定义 UIView 控制器错误后关闭并再次呈现 [英] NSNotificationCenter in custom UIView controller error after dismiss and present again
问题描述
我有一个带有一些视图和自定义 uiview
的视图控制器.由于自定义 uiview,当我用手指触摸屏幕时,我会画一条线.
I have a view controller with some views and a custom uiview
. When I touch the screen with my finger I draw a line thanks to the custom uiview.
为此,我通过通知中心将 location.x
和 location.y
发送到我的自定义 uiview
就像这样
To do that I send location.x
and location.y
through Notification Center to my custom uiview
like that
CGPoint location = [touch locationInView:self.view];
userInfo = [NSMutableDictionary dictionary];
[userInfo setObject:[NSNumber numberWithFloat:location.x] forKey:@"x"];
[userInfo setObject:[NSNumber numberWithFloat:location.y] forKey:@"y"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"addLine" object:self userInfo:userInfo];
在我的自定义 uiview 中,我以这种方式接收所有内容:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addLine:) name:@"addLine" object:nil];
NSDictionary* userInfo = notification.userInfo;
float x = [[userInfo objectForKey:@"x"] floatValue];
float y = [[userInfo objectForKey:@"y"] floatValue];
p = CGPointMake(x,y);
而且效果很好!!!但只是第一次!!!
问题是如果我关闭初始化自定义 uiview 的主视图控制器并返回(例如再次播放),则会出现此错误
The problem is If I dismiss the main viewcontroller where my custom uiview is initialized and I come back (to play again for example) this error appears
[__NSCFType addLine:]: 无法识别的选择器发送到实例0x1454dec0* 由于未捕获的异常NSInvalidArgumentException"而终止应用程序,原因:-[__NSCFType addLine:]:无法识别的选择器发送到实例 0x1454dec0'
[__NSCFType addLine:]: unrecognized selector sent to instance 0x1454dec0 * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType addLine:]: unrecognized selector sent to instance 0x1454dec0'
似乎观察者在解雇后不再工作......你能帮我吗?
It seems that the observer doesn't work again after a dismiss...can you help me?
谢谢
推荐答案
您可能忘记删除 dealloc 中的 oberver
You have probably forgot to remove the oberver in the dealloc
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
这篇关于NSNotificationCenter 在自定义 UIView 控制器错误后关闭并再次呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!