Summarize ios to develop a method for reverse passing values

  • 2020-05-24 06:15:27
  • OfStack

There are many ways to reverse the iOS value transfer. The following is a summary of several common ways to transfer values (only with relevant codes) :

Type 1: the agent passes the value
The second controller:


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

The first controller:


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

Type 2: notification transfer
The first controller:


 // Register monitor notification 
 [[NSNotificationCenter defaultCenter] addObserver:self     selector:@selector(limitDataForModel:) name:@"NOV" object:nil];
- (void)limitDataForModel:(NSNotification *)noti{
self.gamesInfoArray = noti.object;
}

The second controller:


// Send a notification 
 [[NSNotificationCenter defaultCenter]   postNotificationName:@"NOV" object:gameArray];

Type 3: singleton pass
Single is a singleton class and has a string type property titleName
In the second controller:


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

The first controller:


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

Type 4: block transfer
The second controller:


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

The first controller:


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

Type 5: extern pass value
The second controller:


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

The first controller:


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

Type 6: KVO transfer
The first controller:


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

The second controller:


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

In fact, there are many ways to transfer the value, such as NSUserDefaults, first keep the data local, and then read, or write plist and other types of files and then read, and so on many ways, here is not listed! These codes have been written for a long time, and I sorted them out 1 time today, which is still messy. Please forgive me if there is anything wrong or insufficient!


Related articles: