您的位置:首页 > 移动开发 > IOS开发

<iOS小技巧> 返回上级目录操作Goback()方法

2017-07-07 23:34 363 查看
Goback()方法功能:返回上一级界面,通过判断 popViewControllerAnimated 类型是否为空,来判断是present还是pop出来,然后直接做了releaseSelf操作;

- (void)goBack
{
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];
}

releaseSelf()方法功能:用来释放通知内存,和Goback()方法结合,以防忘记释放默认通知;

- (void)releaseSelf
{
//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方法?合成一个方法的弊端在哪里?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐