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];
}
其實還有許多種傳值方法,好比說NSUserDefaults,先把數據堅持在當地,再讀取,或許寫入plist及其它類型的文件再讀取等等很多方法,在這裡就紛歧一羅列了!這些代碼寫的時光比擬久了,明天整頓了一下,還比擬亂,有甚麼纰謬或缺乏的處所請見諒!
【匯總ios開辟逆向傳值的辦法】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!