Iphone NSNotificationCenter postNotificationName exec_badaccess
我有一个视图控制器,当它完成时,我发布一个通知,在另一个视图控制器中包含的子视图中,添加了一个oberserve。但是,当它试图执行post NOTIFICATION方法时,exec_bad_访问发生了。怎么了?代码为:Iphone NSNotificationCenter postNotificationName exec_badaccess,iphone,uiview,viewcontroller,nsnotificationcenter,Iphone,Uiview,Viewcontroller,Nsnotificationcenter,我有一个视图控制器,当它完成时,我发布一个通知,在另一个视图控制器中包含的子视图中,添加了一个oberserve。但是,当它试图执行post NOTIFICATION方法时,exec_bad_访问发生了。怎么了?代码为: BrandListByIdViewController.m - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSNumber
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__);
}
即使我去掉了用户信息,仍然是糟糕的访问。我在另一个新项目中创建了类似的情况,效果很好 不确定这是否是原因,但当您将视图添加到通知中心时,您的选择器错误:
selector:@selector(didSelectedBrandId::)
应该只有一个冒号。整条线路应为:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didSelectedBrandId:) name:@"SelectedBrandId" object:nil];
两个冒号表示该方法需要两个参数,但只需要一个。我不知道您处理的是
UIView
而不是UIViewController
(应该更好地阅读您的问题)。我认为现在的情况是,视图即使在发布后也会收到通知。确保在您的ui视图中调用dealloc
,并将自身作为观察者移除:
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
另外,在UIView
的initWithFrame
方法中放置一个NSLog
,查看它是否被多次调用
这个问题非常相似:
bid
不应发布,因为它是用户信息字典的成员(保留其值并复制其键)。我已尝试在完成Dismise之前放置postNotificationName方法。不管我把密码放在哪里。该视图控制器最终将关闭。我尝试过委派,但似乎不起作用。委托方法不会在uiview中执行。修改了我的答案。我以为你在处理两个视图控制器。代表属于VC。您可以使用委托方法,让拥有该视图的VC来处理它,或者尝试上面修改的答案。