UIViewContrller之间切换的几种方式和传值


1.UIViewContrller之间的切换有三种方式:
    一、UIViewController自身之间的调用:[self presentModalViewController:UIViewController animated:YES];
对应的返回操作函数:[self dismissModalViewControllerAnimated:YES];这里的self代表的是UIViewController自己。
   其可设置四种动画模式:
[java]  view plain copy print ?
  1. case 0:  
  2. [ctrl setModalTransitionStyle:UIModalTransitionStyleCoverVertical]; //底部向上 默认的动画方式  
  3. break;  
  4. case 1:  
  5. [ctrl setModalTransitionStyle:UIModalTransitionStyleCrossDissolve]; //渐变  
  6. break;  
  7. case 2:  
  8. [ctrl setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal]; //翻转  
  9. break;  
  10. case 3:  
  11. [ctrl setModalTransitionStyle:UIModalTransitionStylePartialCurl]; //翻半页  
  12. break;  
  13. default:  
  14. [ctrl setModalTransitionStyle:UIModalTransitionStyleCoverVertical]; //底部向上 默认的动画方式  
  15. break;  

       二、通过UIViewController的子类UINavigationControlle导航控制器的Controller来控制ViewContrller之间的切换,这种切换模式适合于具有层次逻辑性的ViewContrller之间的切换。
对应的调用函数:[self.navigationController pushViewController:ctrl animated:YES]; 
返回:[self.navigationController popViewControllerAnimated:YES];,
也可直接指定返回到那一个ViewController:
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
还可以直接返回到RootViewController:- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated

    实现NavigationController的delegate可以在返回时做相应的更新操作(- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated )

      三、就是UITabBarContrller其也是继承于UIViewController,主要是做具有并行逻辑特点的ViewController之间的切换。

UITabBarContrller对象有一个属性selectedIndex,设置初始时默认展示的ViewController。

而当点击tabBarItem时触发切换操作  都会回调UITabBarControllerDelegate中的一个方法

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *) viewController 


Controller调用了返回函数,如dismiss,pop等等,那么就会被清理掉。
备注:如果Controller或view中有线程或者定时器之类的动画操作如果没有结束那么这个Controller或view就会出现内存泄露了。

方式一(跳转+传值):通过定义一个实体类传值 (从ViewController1 跳转至 ViewController2)

1、定义实体类NotificationEntity

      .h声明文件

     #import <Foundation/Foundation.h>

    @interface NotificationEntity : NSObject

     {

     }

    @property (nonatomic,retainNSString *strTitle; //参数一

    @property (nonatomic,retainNSString *strContent; // 参数二

    @property (nonatomic,retainNSString *strUrl; //参数三

    @end


      

     .m实现文件

     #import "NotificationEntity.h"

     @implementation NotificationEntity

     @synthesize strTitle=_strTitle;

     @synthesize strContent=_strContent;

     @synthesize strUrl=_strUrl;

     @end


2、在ViewController2

      在.h文件中声明实体类NotificationEntity为ViewController2的类成员变量:

      @property (retain,nonatomicNotificationEntity *mNotifEntity;


      在.m文件中通过@synthesize为成员变量mNotifEntity合成存取方法:

       @synthesize mNotifEntity=_mNotifEntity;


3、在ViewController1中实现跳转并传递参数

            // 组装实体类的实例变量

            NotificationEntity *mNotificationEntity = [[NotificationEntity allocinit];

            [mNotificationEntity setStrTitle:strTitle];

            [mNotificationEntity setStrContent:strContent];

            [mNotificationEntity setStrUrl:strUrl];

            // 实例化ViewController2

            ViewController2 *viewController2 = [[ViewController2 allocinit];

            // 注入参数

            [viewController2 setMNotifEntity:mNotificationEntity];

            // 跳转

            [self.window.rootViewController presentModalViewController:viewController2 animated:YES];


4、在ViewController2中接收参数:

        - (void)viewDidLoad

        {

            [super viewDidLoad];

            [self.lblTitle setText:[self.mNotifEntity strTitle]];

            [self.lblContent setText:[self.mNotifEntity strContent]];

        }


5、在ViewController2中加入返回ViewController1的事件:

        - (IBAction)backOff:(id)sender

        {

            [self dismissModalViewControllerAnimated:YES];

        }


切换方式二:xib跳转

在ViewController1中:

1、跳转至ViewController2            

           ViewController2 *viewController2 = [[ViewController2 alloc]init];

           [self presentModalViewController:secondViewanimated:YES];

2、从ViewController2返回至ViewController1:

         - (IBAction)backOff:(id)sender

        {

                  [self dismissModalViewControllerAnimated:YES];

       }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值