<iOS小技巧> 返回上级目录操作Goback()方法
Goback()方法功能:返回上一级界面,通过判断 popViewControllerAnimated 类型是否为空,来判断是present还是pop出来,然后直接做了releaseSelf操作;
- (void)goBack
{
IDSLOG(@"self: %@, parent: %@", self, [self parentViewController]);
id page = [self presentingViewController];
IDSLOG(@"presenting page: %@", page);
{
IDSLOG(@"self: %@, parent: %@", self, [self parentViewController]);
id page = [self presentingViewController];
IDSLOG(@"presenting page: %@", page);
id vc = [self.navigationController popViewControllerAnimated:YES];
IDSLOG(@"pop the = %@", vc);
if (nil == vc) {
[self dismissViewControllerAnimated:YES completion:^{
}];
}
[self releaseSelf];
}
[self dismissViewControllerAnimated:YES completion:^{
}];
}
[self releaseSelf];
}
releaseSelf()方法功能:用来释放通知内存,和Goback()方法结合,以防忘记释放默认通知;
- (void)releaseSelf
{
//sub class implements.
IDSLOG(@"self: %@", self);
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
{
//sub class implements.
IDSLOG(@"self: %@", self);
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
PS:在每个文件前面要加这两句释放内存的语句
- (void)dealloc
{
IDSLOG(@"dealloc - IDSGameRoomHomePage");
}
- (void)releaseSelf
{
[super releaseSelf];
}
二、我的想法
Goback这种方法,使用起来很便捷,又注意了内存泄漏,之前写的时候,每次都要对应Push 或者 present 来写返回操作,现在一个 [self goback] 就搞定了,我觉得这是一个比较便捷又不容易出问题的好方法。
三、思考与行动
1.Goback方法这样写会不会存在问题?如果有,是否思考过更好的解决办法?
2.releaseSelf 和 dealloc 有啥区别?为什么有dealloc还需要releaseSelf方法?合成一个方法的弊端在哪里?