zoukankan      html  css  js  c++  java
  • 简述UIViewControl之间的七种传值方式~~~

    将自己学习到的UIViewControl之间传值的几种方式在这里做一下总结,希望童鞋们多多支持哈~~~

    一.正向传值方式

        这种方式传值应该是最简单的方式,我们先来建立两个视图控制器暂且称为OneViewControl和TwoViewControl,然后第一个视图控制器上面有一个UIButton(按钮)和一个UIlabel(标签),第二个控制器中有一个UIButton和一个UITexField(文本框)。然后我们在AppDelegate加入如下代码

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

        self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

        self.window.backgroundColor = [UIColor whiteColor];

        [self.window makeKeyAndVisible];

        OneViewController *root = [[OneViewController alloc]init];

         self.window.rootViewController = root;

        return YES;

    }

     通俗的说上面的代码就是让程序一运行的时候,首先执行的是OneViewControl中的代码,很简单。

    现在我们想要达到的目的就是,点击OneViewControl中的按钮时,能把这个按钮上面的文字传到TwoViewControl中按钮上面。

    我们在TwoViewControl中.h文件中声明一个属性

    @property(nonatomic,copy)NSString *str;
    

     在OneViewControl中的按钮事件中添加如下代码

    -(void)onClick:(UIButton*)sender
    {
        TwoViewController *twoView = [[TwoViewController alloc]init]; 
        //使用属性传值
        twoView.str = sender.titleLabel.text;
       //跳转到下一个视图,是否有动画,为了简洁,就不写动画了  
        [self presentViewController:twoView animated:YES completion:nil];
    }
    

     好了~这样TwoViewControl中按钮上面的值就跟OneViewControl中按钮值一样了.达到了传值的效果~~~很简单的啦。

    二.使用代理传值(反向传值)

    这次我们在TwoViewControl上面的文本框输入一些内容,然后点击按钮,返回到OneViewControl中,将内容显示到OneViewControl的UILabel上。(所谓反向传值就是从后一个视图控制器传值到前一个视图控制器中)

    先在TwoViewControl中.h文件中声明协议:并声明弱引用指针

    @protocol TwoViewControllerDelegate<NSObject>
    //声明协议
    //在接收方调用
    -(void)inputString:(NSString*)textStr;
    @end
    @interface TwoViewController : UIViewController
    //委托方声明弱引用指针
    @property(nonatomic,weak)id<TwoViewControllerDelegate>delegate;
    @end
    

    在TwoViewControl中.m的文件中按钮事件加入如下代码:

    -(void)onClick
    {
        //找到textField文本
        UITextField *tf = (id)[self.view viewWithTag:2];
        [tf resignFirstResponder];
        //回传数据
        [self.delegate inputString:tf.text];
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    

     在OneViewControl中的按钮方法中加入如下代码

    -(void)onClick:(UIButton*)sender
    {
        TwoViewController *two = [[TwoViewController alloc]init];
        two.delegate = self;
        
        [self presentViewController:two animated:YES completion:nil];
    }
    

     好了第二种代理传值就是这样,~~~别忘记在第一个视图控制器中.m文件加入遵守协议

     三.通知传值(反向传值)

    在TwoViewControl中先创建一个通知对象,并发送通知,在按钮事件中加入如下代码

        //第一个参数是通知的名字,必须填写
        //第二个参数发送的对象
        //第三个参数是字典,携带信息,没有信息传入nil
        NSNotification *noti = [NSNotification notificationWithName:@"myNotification" object:self userInfo:@{@"inputstr":tf.text}];
        
        //发送通知
        [[NSNotificationCenter defaultCenter]postNotification:noti];

              [self dismissViewControllerAnimated:YES completion:nil];

     在OneViewControl中加入监听通知的方法及响应方法

    //3.监听通知
    -(void)viewWillDisappear:(BOOL)animated
    {
        [super viewWillDisappear:animated];
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        //第一个参数是观察者是谁
        //第二个是调用的方法
        //第三个是监听通知的名字
        //通知发送的对象,nil表示任何对象
        [center addObserver:self selector:@selector(receiveNoti:) name:@"myNotification" object:nil];   
    }
    //4.响应
    -(void)receiveNoti:(NSNotification*)noti
    {
        UILabel *label = (id)[self.view viewWithTag:1];
        label.text = noti.userInfo[@"inputstr"];
        
    }
    

     应该注意的是,通知的名称两边必须一致,不然是接收不到发送过来的通知

    四.使用Block传值(反向传值)

     使用Block传值,先声明一个无返回值,有一个参数的Block属性在TwoViewControl中

    @property (nonatomic,copy)void(^returnStrBlock)(NSString*);
    

     在TwoViewControl中,按钮事件中加入如下代码,当前视图调用block

        UITextField *tf = (id)[self.view viewWithTag:2];
        [tf resignFirstResponder];
        
        self.returnStrBlock(tf.text);
        [self dismissViewControllerAnimated:YES completion:nil];
    

     在oneViewControl中按钮事件设置回调的的block函数

        TwoViewController *two = [[TwoViewController alloc]init];
        
        //设置回调的block函数
        two.returnStrBlock = ^(NSString* inputStr)
        {
            UILabel *label = (id)[self.view viewWithTag:1];
            label.text = inputStr;
        };
        [self presentViewController:two animated:YES completion:nil];
    

     这个写的比较简单,但是效果是可以达到的~~~

    五.使用全局变量传值(全局变量传值)

    这种方式我觉得是很low的一种方式,也非常简单,在TwoViewControl和oneViewControl中分别加入下面两句代码就可以了

    NSString *inputStr;
    //引用声明在其他文件中的数据
    extern NSString *inputStr;
    

     我个人是不建议使用这种方式的~~~

    六.单例传值

     这种方式也是比较容易理解的,新建一个类文件.h文件中加入如下代码

    @interface SingletonModel : NSObject
    @property(nonatomic,strong)NSString *textStr;
    
    //声明单例方法
    +(SingletonModel *)shareSingletonModel;
    @end
    

     在.m文件中实现这个类方法(单例模式大部分都是这样创建实例的,简单易懂,反正就一个嘛~~~)

    static SingletonModel *shareObj = nil;
    @implementation SingletonModel
    
    +(SingletonModel *)shareSingletonModel
    {
        if(shareObj==nil)
        {
            shareObj = [[SingletonModel alloc]init];
        }
        return shareObj;
    }
    @end
    

     然后就很简单啦,在TwoViewControl中将值传给单例对象,在OneViewControl中获取这个值就欧啦~~~

     六.使用AppDelegate传值

    简单来说,就是 在AppDelegate中声明一个属性,然后TwoViewControl中创建一个AppDelegate对象

        AppDelegate *appdelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
        
        appdelegate.textStr = tf.text;
    

     然后在OneViewControl中

    -(void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
        AppDelegate *appdelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
        
        UILabel *label = (id)[self.view viewWithTag:1];
        label.text = appdelegate.textStr;
    }
    

     这种方式跟上面的单例模式有着异曲同工之妙,举个很简单的例子,两个人要交换东西,先将东西交给第三个人,再由第三个人转交给两人,而上面的单例和AppDelegate方式传值,都是相当于那个第三个人。

    好啦  ~~~~七种方式总算写完了,从第一个字到最后一个符号,纯手打,代码也是一个个敲的~~~望各位小伙伴多多理解,如果哪里有问题,希望多多交流,如果觉得有用的话,就给个小小的赞啦~~~PS:我这个人很容易满足的~~~谢谢各位小伙伴哈!

     

  • 相关阅读:
    如何将一棵树转化成二叉树
    雪碧图的使用
    CSS简介,引入方式,文字和文本样式设置
    表格Table和表单元素
    html 中< col>标签和< colgroup>标签的区别
    Emmet的HTML语法(敲代码的快捷方式)
    抖音风格字体效果
    几种有效减小电脑系统盘使用量的方法
    ubuntu 机器名称修改方法
    Ubuntu 为基于X应用程序增加启动项的正确做法
  • 原文地址:https://www.cnblogs.com/yy342901/p/4902776.html
Copyright © 2011-2022 走看看