iOS开发之presentViewController、presentModelViewController和pushViewController

本文介绍了一种iOS开发中遇到的问题:使用UINavigationController时,从一个ViewController push到另一个ViewController后无法正确pop回前一个视图。文章通过分析代码和调试过程找到了问题所在,并给出了正确的实现方式。

本来这个应该是昨天发出来的,昨天时间来不及就放到今天发了。

昨天在开发中遇到一个问题:从ViewController push一个视图,返回的时候返回不了;查看代码发现push方法、pop方法都没问题;话不多说,直接上代码:

- (void)AddNewFeedBacks

{

FeedBackViewController *feedBackView = [[FeedBackViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:feedBackView];
[self.navigationController presentViewController:nav animated:YES completion:nil];
}

- (void)popBack

{

    [self.navigationController popViewControllerAnimated:YES];

}

UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"reback.png"] style:UIBarButtonItemStylePlain target:self action:@selector(popBack)];

self.navigationItem.leftBarButtonItem = leftItem;

方法控件都没问题;这时候就不得不断点来看,结果发现到popBack方法的时候这个方法没生效,于是查看了navigationController的一些push、pop方法结果发现了问题出在退出视图,也就是[self.navigationController presentViewController:nav animated:YES completion:nil];这里。

然后去查看了下相关api发现这两个方法的区别:

这两个方法都是必须要ViewController, 是建立在UINavigationController之上的。
SQLiteViewController *baSQLiteViewController = [[SQLiteViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:baSQLiteViewController];
[self.window addSubview:nav.view];

推出视图push,下一页面会自动添加返回按钮
InforViewController *baInforViewController = [[InforViewController alloc] init];
[self.navigationController pushViewController:baInforViewController animated:YES];

 

以Modal方式推出一个视图,弹出的视图没有返回按钮,需要自己添加
NewDataViewController *baNewViewController = [[NewDataViewController alloc] init];
UINavigationController *baNavigation = [[UINavigationController alloc] initWithRootViewController:baNewViewController];
[self.navigationController presentModalViewController:baNavigation animated:YES];
[baNewViewController release];
[baNavigation release];

为弹出视图添加返回按钮
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(returnView)];

返回方法
- (void)returnView

{
    [self.parentViewController dismissModalViewControllerAnimated:YES];
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值