(iPhone/iPad开发)present某一个ViewController

本文介绍了一种在iOS应用中使用UINavigationController时避免内存泄漏的方法。通过直接在presentViewController方法中添加并配置UINavigationController,确保在dismissViewController后能正确释放内存。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天试图实现这么一个功能:通过presentViewController呈现某一个viewController,并且在这个页面中添加一个UINavigationController,可以达到在这个页面进行逐步跳转页面功能。


最初我是在跳转后的页面的initWithNibName等初始化函数添加UINavigationController,然后设置RootViewController为self,这样是实现了可以从当前界面跳转到其他界面,但是很快发现一个问题,当在界面中执行dismissViewController时,不会自动执行这个界面的dealloc()函数,也就意味着,内存中并没有释放之前present的那个界面,那个界面在添加UINavigationController时被retain了,可是几经尝试,在那个界面最终还是不容易自动执行dealloc()函数。


最后,我的解决方法是,在需要presentViewController处,添加UINavigationController,并设置rootViewController为需要present的view,present时将navigation那个对象传过去。具体实现代码如下:


#pragma mark - Table view delegate


//往视图中添加ViewController


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

      if(indexPath.section ==0)

      {

          if (indexPath.row ==1) {

              WeChatMineNameViewController * WeChatMinrNameView= [[[WeChatMineNameViewController alloc] init]autorelease];

              UINavigationController * WeChatMinrNameNav = [[[UINavigationController alloc] initWithRootViewController:WeChatMinrNameView]autorelease];

              [self presentViewController:WeChatMinrNameNav animated:YES completion:nil];

          }

      }

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值