yon*_* ho 4 iphone uiview viewcontroller nsnotificationcenter
我有一个视图控制器,当它完成时,我发布了一个notfication,并且在另一个视图控制器中包含的子视图中添加了一个oberserve.但是,当它试图执行post notificaiton方法时,exec_bad_access会发生.怎么了?代码是:
BrandListByIdViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSNumber *bid = self.brands[indexPath.row][@"id"];
[self dismissViewControllerAnimated:YES completion:^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"SelectedBrandId" object:nil];
}];
}
SearchNewProduct.h
@interface SearchNewProduct : UIView
@end
SearchNewProduct.m
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didSelectedBrandId::) name:@"SelectedBrandId" object:nil];
}
}
- (void)didSelectedBrandId:(NSNotification *)notif{
NSLog(@"%s", __PRETTY_FUNCTION__);
}
Run Code Online (Sandbox Code Playgroud)
即使我摆脱了userInfo,仍然是糟糕的访问.我在另一个新项目中创建了类似的情况,它工作正常.
我没有意识到你正在处理a UIView而不是a UIViewController(应该更好地阅读你的问题).我认为正在发生的事情是View即使在发布后也会收到通知.一定要打电话给dealloc你,UIView并以观察者的身份将其移除:
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Run Code Online (Sandbox Code Playgroud)
此外,把一个NSLog在UIView的initWithFrame方法,看它是否被调用一次以上.
这个问题非常相似:
| 归档时间: |
|
| 查看次数: |
3520 次 |
| 最近记录: |