转】视图切换总结

本文介绍了iOS应用开发中视图切换的多种方式,包括使用UINavigationController进行导航切换、使用ModalViewController进行模态视图切换等,并提供了具体的实现代码示例。

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

1、利用ModalViewController切换View


当程序中含有多个 view,需要在之间切换的时候,可以使用 UINavigationController,或者是 ModalViewController。UINabigationController 是通过向导条来切换多个 view。而如果 view 的数量比较少,且显示领域为全屏的时候,用 ModalViewController 就比较合适(比如需要用户输入信息的view,结束后自动回复到之前的view)。
ModalViewController 并不像 UINavigationController 是一个专门的类,使用 UIViewController 的 presentModalViewController 方法指定之后就是 ModalViewController 了。
调用 UIViewController 的 dismissModalViewController:Animated: 方法就可以退出


在A里面直接跳到B中 [A presentModalViewController:B animated:YES];
在B中可以通过 返回到A [self dismissModalViewControllerAnimated:YES]


UINavigationController 的 pushViewController: animated:
UIViewController的presentModalViewController: animated:


增加 Notification
post Notification


addSubview
b bringSubviewToFront
insertSubview b atIndex 0
removeview a


IPhone中视图切换,网上常见的方法都是用viewcontrol,在viewcontrol中插入不同的视图实现,见下面的代码
[self.view insertSubview:self.mainview.view atIndex:0];
只要切换不同的mainview就可以实现,网上常见的方法也是如此,不过我却想用另外一种方法,就是直接操作window,
在window中切换view,实现方法如下:
[viewController.view removeFromSuperview];
[window addSubview:viewset.view];
其中viewController是当前窗口,首先从 父窗口中移除,然后调用window addSubview方法即可。
不过使用第二个方法,出现了一个问题就是原来的主窗口显示正常,但是切换到第二个窗口是,窗口被电池栏给覆盖了,显得很奇怪,目前无解。


1.首先用pushViewController切换到下一个视图的,切换回来用popViewControllerAnimated
也就是:
切换视图:[self.tabBarController.navigationController pushViewController:_appMore animated:YES];
或者[self.navigationController pushViewController:_appMore animated:YES];
切换回来的时候用:
[self.navigationController popViewControllerAnimated:YES];

2.两个view之间切换:
假设aimview是目标视图
currentview是当前视图
首先移除当前view
[currentview.view removeFromSuperview];
[self.view insertSubview:aimview.view atIndex:0]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值