在UIViewController里面注册通知,页面消失时移除通知。你这边可要注意了,一定要成双成对出现,如果你只在
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(maidanPay:) name:TopUpBlock object:nil];
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:TopUpBlock object:nil];
}
viewWillAppear 中 addObserver没有在viewWillDisappear
中 removeObserver那么当消息发生的时候,你的方法会被调用多次,这点必须牢记在心。-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(maidanPay:) name:TopUpBlock object:nil];
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:TopUpBlock object:nil];
}
本文介绍在iOS应用开发中如何正确地使用NSNotification进行消息监听,并强调在UIViewController中注册与取消注册通知的重要性,以避免内存泄漏和重复回调的问题。
1440

被折叠的 条评论
为什么被折叠?



