背景
项目需要,为了实现类似于增添联系人的效果:
在项目中,增加联系人后,返回联系人列表,刷新刚刚联系人。
简单来说,就是在返回的时候,刷新数据。
实现的方式有很多种,我分2步完成:
1.提供刷新数据及动画的方法。这个不是本文重点,不予赘述。
2.返回时,调用刷新及动画方法。
关于这个,我选择使用UINavigationControllerDelegate中的方法:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
具体来说,就是加了这么一句代码:
self.navigationController.delegate = self;
然后,实现上面提到的delegate,在其中调用刷新数据及动画的方法。
问题
点赞返回之后,再push或者pop到其他页面,都会crash。
而且,模拟器下crash,还没有显示crash原因。
如何解决?
查看真机中该app日志,才发现是EXC_BAD_ACCESS
这个问题。
多么熟悉的提示,野指针异常!
既然是野指针,而且,是在添加了delegate之后出现,那十有八九就是它的问题了。
再查看了delegate这个属性的定义。
@property(nonatomic, assign) id<UINavigationControllerDelegate> delegate;
看到assign
的时候,就知道了原因:
返回时,该controller已经被销毁了。
而navigation controller的delegate还指向已经被销毁的controller,即指向nil。
野指针妥妥的!
最后,在viewWillDisappear:
中将 delegate 置为nil
,如下:
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
self.navigationController.delegate = nil;
}
完美解决!
总结
- delegate一般得用
weak
标识符,这样当delegate指向的controller被销毁时,delegate会跟着被置为nil,可以有效防止这种问题。 - 若是使用
assign
标识的delegate,则注意在delegate指向的对象被销毁时,将delegate 置为nil。 - 也有不将delegate置为nil,没有问题的情况。如常见的tableView,其delegate和datasource,一般不会在其他controller中使用该tableView,所以不会有这种问题。
本人还是iOS开发初学者,以上为个人见解,有写得不对的,欢迎指出,不胜感激!
demo:
- (void)viewDidLoad {
[superviewDidLoad];
self.appDelegate = [UIApplicationsharedApplication].delegate;
self.tableView.tableFooterView = [[UIViewalloc] init];
}
- (void)viewWillDisappear:(BOOL)animated {
[superviewWillDisappear:animated];
self.navigationController.delegate =nil;
}
//当导航视图控制器控制某个视图显示器显示出来时,激发该方法
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
if ([selfisKindOfClass:[UITableViewControllerclass]]) {
[viewController viewWillAppear:animated];
//控制表格重新加载数据
[((UITableViewController *)viewController).tableViewreloadData];
}
}
- (void)viewWillAppear:(BOOL)animated{
[superviewWillAppear:animated];
self.navigationController.delegate =self;
//创建抓取数据的请求对象
NSFetchRequest *request = [[NSFetchRequestalloc] init];
//设置要抓取哪种类型的实体
NSEntityDescription *entity = [NSEntityDescriptionentityForName:@"FKAuthor"inManagedObjectContext:self.appDelegate.managedObjectContext];
[request setEntity:entity];
NSError *error =nil;
//执行抓取数据的请求,返回符合条件的数据
authorArray = [[self.appDelegate.managedObjectContextexecuteFetchRequest:request error:&error] mutableCopy];
if (authorArray ==nil) {
NSLog(@"获取FKAuthor实体时出错:%@,%@",error,[erroruserInfo]);
}
// [self.tableView reloadData];
}