<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方法?合成一个方法的弊端在哪里?
- (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方法?合成一个方法的弊端在哪里?
相关文章推荐
- C#路径/文件/目录/I/O常见操作汇总<转载>
- C#操作字符串方法总结<转>
- C#中List<object>.Clear()方法和实例化new List<object>()操作的结果分析
- IEnumerable<T>作为方法返回值类型——建议通过yield return返回
- iOS基础控件-UITabBarController - 3 之<UITabBarControllerDelegate>协议中的四个重要方法
- Java中使用hql,sql查询返回的list<Object> 转成需要的实体对象--方法讲解!
- “无法更新EntitySet“*****”,因为它有一个DefiningQuery,而<ModificationFunctionMapping>元素中没有支持当前操作的<InsertFunction>元素”问题的解决方法
- 关于C++头文件#include<iostream.h>debug时提示文件目录不存在错误解决方法?
- iOS- <项目笔记> UIApplication常见属性与方法总结
- Cocos2d-x--iOS平台lua加密成luac资源方法和Jsc文件<MAC平台开发试用--windows平台暂未研究>
- 解决 ”不允许在查询中显式构造实体类型“问题及使用其他方法实现返回 List<Model对象>或者IQueryable<Model对象>对象
- 调试自定义操作/安装程序类的方法<转>
- ios菜鸟之路:<a href ="http://www.baidu.com">xxxx</a>提取xxxx的3种方法
- iOS - 操作文件目录的方法
- IOS中Notificaiton通知的使用方法<代码演示>
- 讨论exe获取dll提供的单例,并获取单例所提供的带有vector<class A>& STL容器的返回值的情况-提供1种解决方法
- C#操作字符串方法总结<转>
- iOS - 操作文件目录的方法
- C#操作字符串方法总结<转>
- iOS - 操作文件目录的方法