iOS反向传值整理

作者: 断剑 | 来源:发表于2016-07-27 22:57 被阅读1661次
    传值展示

    本文主要讲解iOS中常见的反向传值方法。

    1.AppDelegate传值

    • 在AppDelegate中定义相关的属性变量
    • 第二个控制器中为AppDelegate中的属性赋值
    • 第一个控制器中在使用的地方取出AppDelegate中的值赋值
    //UIApplicationDelegate
    @interface AppDelegate : UIResponder <UIApplicationDelegate>
    @property (strong, nonatomic) UIWindow *window;
    //AppDelegate属性
    @property (nonatomic, copy) NSString * string;
    @property (nonatomic, strong) UIColor * color;
    @end
    
    //第二个控制器
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        //设置数据信息
        AppDelegate * app = [UIApplication sharedApplication].delegate;
        
        app.string = self.field.text;
        app.color = [UIColor orangeColor];
        
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    //第一个控制器---设置
     AppDelegate * app = [UIApplication sharedApplication].delegate;
        
        if (app.string) {
            [self.appBtn setTitle:app.string forState:UIControlStateNormal];
            [self.appBtn setBackgroundColor:app.color];
        }
    

    2.代理传值

    • 第二个控制器设置代理相关信息
    • 第一个控制器实现代理
    //第二个控制器
    #import <UIKit/UIKit.h>
    
    @protocol ZZYDelegateViewControllerDelegate <NSObject>
    
    @optional
    
    - (void)delegateViewControllerDidClickwithString:(NSString *)string color:(UIColor *)color;
    
    @end
    
    @interface ZZYDelegateViewController : UIViewController
    
    @property (nonatomic, assign) id<ZZYDelegateViewControllerDelegate> delegate;
    
    @end
    
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        if ([self.delegate respondsToSelector:@selector(delegateViewControllerDidClickwithString:color:)]) {
            [self.delegate delegateViewControllerDidClickwithString:self.field.text color:[UIColor greenColor]];
        }
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    //第一个控制器
    
    @interface ZZYRootViewController ()<ZZYDelegateViewControllerDelegate>
    
    @property (weak, nonatomic) IBOutlet UIButton *delegateBtn;
    
    @end
    
    - (IBAction)pressDelegateVc:(id)sender {
        ZZYDelegateViewController * vc = [[ZZYDelegateViewController alloc]init];
        vc.delegate = self;
        [self.navigationController pushViewController:vc animated:YES];
    }
    - (void)delegateViewControllerDidClickwithString:(NSString *)string color:(UIColor *)color
    {
        [self.delegateBtn setTitle:string forState:UIControlStateNormal];
        [self.delegateBtn setBackgroundColor:color];
    }
    

    3.控制器传值

    • 第一个控制器.h里面设置相关属性等信息
    • 第二个控制器设置第一个控制器参数,并在对应位置设置相关信息
    //第一个控制器
    #import <UIKit/UIKit.h>
    
    @interface ZZYRootViewController : UIViewController
    
    @property (weak, nonatomic) IBOutlet UIButton *vcBtn;
    
    @end
    
    //第二个控制器
    #import <UIKit/UIKit.h>
    
    @class ZZYRootViewController;
    
    @interface ZZYVCViewController : UIViewController
    
    @property (nonatomic, strong) ZZYRootViewController * rootVc;
    
    @end
    
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        [self.rootVc.vcBtn setTitle:self.field.text forState:UIControlStateNormal];
        [self.rootVc.vcBtn setBackgroundColor:[UIColor orangeColor]];
        [self.navigationController popViewControllerAnimated:YES];
    }
    

    4.通知传值

    • 第二个控制器发送通知
    • 第一个控制器接收通知,设置数据
    //第二个控制器
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
     
        [[NSNotificationCenter defaultCenter]postNotificationName:@"ZZYNotiSendValue" object:nil userInfo:@{@"value":self.field.text,@"color":[UIColor redColor]}];
    
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    //第一个控制器
      [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(setNoti:) name:@"ZZYNotiSendValue" object:nil];
    - (void)setNoti:(NSNotification *)noti
    {
        
        [self.notiBtn setTitle:noti.userInfo[@"value"] forState:UIControlStateNormal];
        [self.notiBtn setBackgroundColor:noti.userInfo[@"color"]];
        
    }
    

    5.Block传值

    • 第二个控制器设置block相关信息
    • 第一个控制器调用block,设置数据
    方式一:
    
    //第二个控制器
    @interface ZZYBlockViewController : UIViewController
    
    @property (nonatomic, copy) void(^ZZYMyBlock) (NSString * string , UIColor * color);
    
    @end
    
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        if (self.ZZYMyBlock) {
            self.ZZYMyBlock(self.field.text,[UIColor lightGrayColor]);
        }
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    //第一个控制器
     ZZYBlockViewController * vc = [[ZZYBlockViewController alloc]init];
       
     [vc setZZYMyBlock:^(NSString * string, UIColor * color) {
            
            [self.blockBtn setTitle:string forState:UIControlStateNormal];
            [self.blockBtn setBackgroundColor:color];
            
        }];
    
    方式二:
    //第二个控制器
    typedef void(^ZZYMySecBlock) (NSString * string , UIColor * color);
    
    @interface ZZYBlockViewController : UIViewController
    
    @property (nonatomic, copy) ZZYMySecBlock mySecondBlock;
    
    - (void)returnTextBlock:(ZZYMySecBlock)block;
    
    @end
    
    - (void)returnTextBlock:(ZZYMySecBlock)block
    {
        self.mySecondBlock = block;
    }
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        if (self.mySecondBlock) {
            self.mySecondBlock(self.field.text,[UIColor lightGrayColor]);
        }
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    //第一个控制器
     ZZYBlockViewController * vc = [[ZZYBlockViewController alloc]init];
    
      [vc returnTextBlock:^(NSString *string, UIColor *color) {
            [self.blockBtn setTitle:string forState:UIControlStateNormal];
            [self.blockBtn setBackgroundColor:color];
        }];
    
    
    3.方式三:
    //第二个控制器
    #import <UIKit/UIKit.h>
    
    typedef void(^ZZYMyThrBlock) (NSString * string , UIColor * color);
    
    @interface ZZYBlockViewController : UIViewController
    
    @property (nonatomic, copy) ZZYMyThrBlock myThrBlock;
    
    @end
    
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
    
       if (self.myThrBlock) {
           self.myThrBlock(self.field.text,[UIColor lightGrayColor]);
       }
       
       [self.navigationController popViewControllerAnimated:YES];
    }
    
    //第一个控制器
    ZZYBlockViewController * vc = [[ZZYBlockViewController alloc]init];
       
       vc.myThrBlock = ^(NSString * string, UIColor * color){
           [self.blockBtn setTitle:string forState:UIControlStateNormal];
           [self.blockBtn setBackgroundColor:color];
    
       };
       [self.navigationController pushViewController:vc animated:YES];
    
    

    注:上述方式一般首选Block传值,在上述Block的三种方式中,第一种和第三种方式使用的时候需要注意补全block的信息

    • 除了上述方式反向传值还可以通过单例、NSUserDefaults方式来进行传值
      示例代码地址

    相关文章

      网友评论

      • 暗香有独:不错,虽然都是最近基本,但也是最重要的,加油楼主。
        断剑:@Daojiao_Boy 谢谢
      • 小白谈理财:感谢分享
        断剑:@那天原来是你啊 互相学习
      • kinmo:不错,弄得挺好的
        断剑:@July丶ye 多谢支持捧场
      • 鲁鲁修:写的很好 顶一下
        断剑: @鲁鲁修 多谢支持
      • mingmingsky:楼主,那个代理传值是不是忘了设置委托人是第一个控制器了
        断剑:@mingmingsky 这里只要没有造成循环引用就不会导致内存泄漏的。如果你不确定的话,也可以使用Xcode的工具检测一下
        断剑:@mingmingsky 恩,是的。那段代码忘记贴上来了
        mingmingsky:@mingmingsky 还有那个控制器传值会造成内存泄露吗
      • kuazi:appdelegate传值,就是单例传值。
        断剑:@kuazi 恩,你可以单独创建一个单例对象来进行传值
      • cba8dfa3f0f4:博主,加油

      本文标题:iOS反向传值整理

      本文链接:https://www.haomeiwen.com/subject/uczojttx.html