zoukankan      html  css  js  c++  java
  • 逆向传值

    iOS的逆向传值有很多种方法,下面来总结几种常用的传值方式(只贴相关代码):

    第一种:代理传值
    第二个控制器:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    @protocol WJSecondViewControllerDelegate <NSObject>
    - (void)changeText:(NSString*)text;
    @end
     @property(nonatomic,assign)id<WJSecondViewControllerDelegate>delegate;
     
    - (IBAction)buttonClick:(UIButton*)sender {
    _str = sender.titleLabel.text;
    [self.delegate changeText:sender.titleLabel.text];
    [self.navigationController popViewControllerAnimated:YES];
    }

    第一个控制器:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    - (IBAction)pushToSecond:(id)sender {
    WJSecondViewController *svc = [[WJSecondViewController alloc]initWithNibName:@"WJSecondViewController" bundle:nil];
    svc.delegate = self;
    svc.str = self.navigationItem.title;
    [self.navigationController pushViewController:svc animated:YES];
    [svc release];
    }
    - (void)changeText:(NSString *)text{
    self.navigationItem.title = text;
    }

    第二种:通知传值
    第一个控制器:

    1
    2
    3
    4
    5
    //注册监听通知
     [[NSNotificationCenter defaultCenter] addObserver:self     selector:@selector(limitDataForModel:) name:@"NOV" object:nil];
    - (void)limitDataForModel:(NSNotification *)noti{
    self.gamesInfoArray = noti.object;
    }

    第二个控制器:

    1
    2
    //发送通知
     [[NSNotificationCenter defaultCenter]   postNotificationName:@"NOV" object:gameArray];

    第三种:单例传值
    Single是一个单例类,并且有一个字符串类型的属性titleName
    在第二个控制器:

    1
    2
    3
    4
    5
    - (IBAction)buttonClick:(UIButton*)sender {
    Single *single = [Single sharedSingle];
    single.titleName = sender.titleLabel.text;
    [self.navigationController popViewControllerAnimated:YES];
    }

    第一个控制器:

    1
    2
    3
    4
    5
    - (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    Single *single = [Single sharedSingle];
    self.navigationItem.title = single.titleName;
    }

    第四种:block传值
    第二个控制器:

    1
    2
    3
    4
    5
    6
    @property (nonatomic,copy) void (^changeText_block)(NSString*);
    - (IBAction)buttonClick:(UIButton*)sender {
    _str = sender.titleLabel.text;
    self.changeText_block(sender.titleLabel.text);
    [self.navigationController popViewControllerAnimated:YES];
    }

    第一个控制器:

    1
    2
    3
    4
    5
    6
    7
    8
    - (IBAction)pushToSecond:(id)sender {
    WJSecondViewController *svc = [[WJSecondViewController alloc]initWithNibName:@"WJSecondViewController" bundle:nil];
    svc.str = self.navigationItem.title;
    [svc setChangeText_block:^(NSString *str) {
      >self.navigationItem.title = str;
    }];
    [self.navigationController pushViewController:svc animated:YES];
    }

    第五种:extern传值
    第二个控制器:

    1
    2
    3
    4
    5
    extern NSString *btn;
    - (IBAction)buttonClick:(UIButton*)sender {
    btn = sender.titleLabel.text;
    [self.navigationController popViewControllerAnimated:YES];
    }

    第一个控制器:

    1
    2
    3
    4
    5
    NSString *btn = nil;
    - (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationItem.title = btn;
    }

    第六种:KVO传值
    第一个控制器:

    1
    2
    3
    4
    5
    6
    - (void)viewDidLoad {
    [super viewDidLoad];
     _vc =[[SecondViewController alloc]init];
    //self监听vc里的textValue属性
    [_vc addObserver:self forKeyPath:@"textValue" options:0 context:nil]; 
    }

    第二个控制器:

    1
    2
    3
    4
    - (IBAction)buttonClicked:(id)sender {
    self.textValue = self.textField.text;
    [self.navigationController popViewControllerAnimated:YES];
    }

    其实还有很多种传值方式,比如说NSUserDefaults,先把数据保持在本地,再读取,或者写入plist及其它类型的文件再读取等等许多方式,在这里就不一一列举了!这些代码写的时间比较久了,今天整理了一下,还比较乱,有什么不对或不足的地方请见谅!

  • 相关阅读:
    Cross-Site Scripting XSS 跨站攻击全攻略 分类: 系统架构 2015-07-08 12:25 21人阅读 评论(2) 收藏
    WAS集群:记一次Node Agent不活动问题解决过程
    Oracle RAC集群资料收集
    Linux使用 tar命令-g参数进行增量+差异备份、还原文件
    WAS7.0安装补丁升级程序无法替换文件 java/docs/autorun.inf解决办法
    Java程序员面试失败的5大原因
    Lemon OA第4篇:常用功能
    Lemon OA第3篇:核心功能
    Lemon OA第2篇:功能解析方法
    Activiti5小试牛刀demo流程
  • 原文地址:https://www.cnblogs.com/LGX3399577/p/sf.html
Copyright © 2011-2022 走看看