zoukankan      html  css  js  c++  java
  • 逆向传值的几种方法整理 iOS

    第一种:代理传值

    第二个控制器:

    @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];
    }

    第一个控制器:

    - (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;
    }

    第二种:通知传值

    第一个控制器:

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

    第二个控制器:

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

    第三种:单例传值

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

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

    第一个控制器:

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

    第四种:block传值

    第二个控制器:

    @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];
    }

    第一个控制器:

    - (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传值

    第二个控制器:

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

    第一个控制器:

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

    第六种:KVO传值

    第一个控制器:

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

    第二个控制器:

    - (IBAction)buttonClicked:(id)sender {
    self.textValue = self.textField.text;
    [self.navigationController popViewControllerAnimated:YES];
    }
    在北京的灯中,有一盏是我家的。这个梦何时可以实现?哪怕微微亮。北京就像魔鬼训练营,有能力的留,没能力的走……
  • 相关阅读:
    group having条件找max无记录问题
    Apache Http Server
    Google产品
    AES加密报错Illegal key size
    内网调试微信开发
    试用VSCode
    React的Transaction浅析
    一个webpack,react,less,es6的DEMO
    20151128
    React生命周期浅析
  • 原文地址:https://www.cnblogs.com/huangzs/p/14684567.html
Copyright © 2011-2022 走看看