匯總ios開發(fā)逆向傳值的方法
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;
}
第二種:通知傳值
第一個控制器:
//注冊監(jiān)聽通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(limitDataForModel:) name:@"NOV" object:nil];
- (void)limitDataForModel:(NSNotification *)noti{
self.gamesInfoArray = noti.object;
}
第二個控制器:
//發(fā)送通知 [[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監(jiān)聽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及其它類型的文件再讀取等等許多方式,在這里就不一一列舉了!這些代碼寫的時間比較久了,今天整理了一下,還比較亂,有什么不對或不足的地方請見諒!
相關文章
Xcode 9下適配iPhoneX導致iOS 10不兼容問題的解決方法
這篇文章主要給大家介紹了關于Xcode 9下適配iPhoneX導致iOS 10不兼容問題的解決方法,文中通過圖文介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2018-04-04
iOS從App跳轉至系統(tǒng)設置菜單各功能項的編寫方法講解
這篇文章主要介紹了iOS從App跳轉至系統(tǒng)設置菜單各功能項的編寫方法講解,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-04-04
iOS9 系統(tǒng)分享調用之UIActivityViewController
UIActivityViewController類是一個標準的view controller,通個使用這個controller,你的應用程序就可以提供各種服務。本文給大家介紹iOS9 系統(tǒng)分享調用之UIActivityViewController,感興趣的朋友一起學習吧2015-11-11
NSURLSession跨域重定向透傳HTTP Header問題解決
這篇文章主要為大家介紹了NSURLSession跨域重定向透傳HTTP Header問題解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
iOS App開發(fā)中Core Data框架基本的數據管理功能小結
除了使用SQL關系型數據庫,我們還可以使用Xcode中提供的Core Data來進行表結構數據處理,這里我們就來初步整理iOS App開發(fā)中Core Data框架基本的數據管理功能小結:2016-06-06

