14、开发逆向传值的几种方法

2019-08-03  本文已影响0人  十二月_9d09

1、代理传值

(1)第二个控制器:

@protocol WjSecondViewcontrollerDelegate<NSObject>
-(void)changeText:(NSString*)text;
@end
@proporty(nonatomic,assign) id<WjSecondViewcontrollerDeleagate>delagate;
-(IBAction*)buttonClick(UIButton* sender){
_str = sender.titleLabel.text;
[self.delegate changeText:sender.titleLabel.text];
self.navigationController popViewControllerAnimated:YES];
}

(2)第一个控制器:

-(IBAction*)pushToSecond:(id) sender{
WjSecondViewcontroller* secondVc = [[WjSecondViewcontroller alloc] initWithNibName:@"WjSecondViewcontroller" bundle:nil];
secondVc.str = self.navigationItem.title;
secondVc.delegate = self;
[self.navigationController pushViewController:secondVc animated:YES];
}
-(void)changeText:(NSString*)text{
self.navigationItem.title = text;
}

二、通知传值

[[NotificationCenter defaultCenter] addObserver:self selector:@selector(notifyMethod:) name:@"notifyName" object:nil];
-(void)notifyMethod:(notification* noti){
   self.gameArray = noti.object;
}
[[NotificationCenter defaultCenter] postNotificationName:@"notifyName" object:gameArray];

三、block传值

第二个控制器:

@proporty(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)pushToSecondViewController:(id)sender{
SecondViewController* secondVC = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
secondVC.str = self.navigationItem.title;
secondVC setChangeText_block:^(NSString* str){
  self.navigationItem.title = str;
}
[self.navigationController pushViewController:secondVC animated:YES];
}

第四、extern 传值

第二个控制器

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

第一个控制器

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

第五、kvo传值

第一个控制器

-(void)viewDidLoad{
  [super viewDidLoad];
 _vc = [[secondViewController alloc]init];
[_vc addObserver:self forKeyPath:@"textValue" opinion:0 context:nil];
}

第二个控制器

-(IBAction)buttonClick:(id)sender{
  self.textValue = self.textField.text;
 [self.navigationController popViewControllerAnimated:YES];
}
上一篇 下一篇

猜你喜欢

热点阅读