標籤:

iOS頁面間傳值的方式

iOS頁面間傳值的方式(NSUserDefault/Delegate/NSNotification/Block/單例)實現了以下iOS頁面間傳值:1.委託delegate方式;2.通知notification方式;3.block方式;4.UserDefault或者文件方式;5.單例模式方式;6.通過設置屬性,實現頁面間傳值在iOS開發中,我們經常會遇到頁面間跳轉傳值的問題,現歸納總結一下:情況1:A頁面跳轉到B頁面方法:在B頁面的控制器中,編寫對應的屬性,在A頁面跳轉到B頁面的地方,給B的屬性賦值即可view sourceprint?1.//SecondViewController.hview sourceprint?1.@property(nonatomic) NSInteger flag;//當前系統標示(0:其他傳值方式;1:block傳值方式)在A頁面的試圖控制器中view sourceprint?1.//RootViewController.mview sourceprint?1.- (IBAction)showSecondView:(id)sender {2.SecondViewController *second = [[SecondViewController alloc] initWithNibName:@"SecondViewController"bundle:nil];3.second.delegate = self;4.second.flag =0;5.[self presentViewController:second animated:YES completion:nil];6.}情況2:A頁面跳轉到B頁面,B頁面再跳轉回A頁面主流方案:(1)通過委託delegate的方式實現

設置協議及方法 view sourceprint?1.view sourceprint?1.//SecondViewController.hview sourceprint?1.@protocolsecondViewDelegate2.-(void)showName:(NSString *)nameString;3.@end設置代理(為防止循環引用,此處採用了weak)view sourceprint?1.view sourceprint?1.//SecondViewController.hview sourceprint?1.@interfaceSecondViewController : UIViewController2.@property(nonatomic, weak)id<secondViewDelegate> delegate;3.@property(nonatomic, copy) ablock block;4.@end調用view sourceprint?01.//SecondViewController.m02.- (IBAction)delegateMethod:(id)sender {03.if([self notEmpty]) {04.[self.delegate showName:self.nameTextField.text];05.[self dismissViewControllerAnimated:YES completion:nil];06.}else{07.[self showAlert];08.}09.}顯示 view sourceprint?1.//RootViewController.m2.-(void)showName:(NSString *)nameString{3.self.nameLabel.text = nameString;4.}最重要也是最容易忽略的,就是一定要設置delegate的指向。 (2)通過通知notification的方式實現

在B頁面的控制器中,發送通知:view sourceprint?01.//SecondViewController.m02.- (IBAction)notificationMethod:(id)sender {03.if([self notEmpty]) {04.[[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeNameNotification"object:self userInfo:@{@"name":self.nameTextField.text}];05.[self dismissViewControllerAnimated:YES completion:nil];06.}else{07.[self showAlert];08.}09.}在A頁面的控制器中,註冊通知:view sourceprint?1.//RootViewController.m2.- (void)viewDidLoad3.{4.[superviewDidLoad];5.// Do any additional setup after loading the view from its nib.6.[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ChangeNameNotification:) name:@"ChangeNameNotification"object:nil];7.}當我們不使用時,要記得刪掉通知:view sourceprint?1.//RootViewController.m2.-(void)dealloc{3.[[NSNotificationCenter defaultCenter] removeObserver:self];4.}調用,顯示view sourceprint?1.//RootViewController.m2.3.-(void)ChangeNameNotification:(NSNotification*)notification{4.NSDictionary *nameDictionary = [notification userInfo];5.self.nameLabel.text = [nameDictionary objectForKey:@"name"];6.}(3)block方式實現block介紹:http://blog.csdn.net/totogo2010/article/details/7839061鏈接一篇描述block回調挺有意思的文章:http://blog.csdn.net/mobanchengshuang/article/details/11751671分析:在B試圖控制器中,定義一個block,參數為字元串view sourceprint?1.//SecondViewController.h2.typedefvoid(^ablock)(NSString *str);view sourceprint?1.//SecondViewController.h2.3.@property(nonatomic, copy) ablock block;在B試圖控制器中,當輸入名字,點擊對應的確定按鈕後view sourceprint?01.- (IBAction)blockMethod:(id)sender {02.if([self notEmpty]) {03.if(self.block) {04.self.block(self.nameTextField.text);05.[self dismissViewControllerAnimated:YES completion:nil];06.}07.}else{08.[self showAlert];09.}10.}在A試圖顯示,回調blockview sourceprint?1.- (IBAction)showSecondWithBlock:(id)sender {2.SecondViewController *second = [[SecondViewController alloc] initWithNibName:@"SecondViewController"bundle:nil];3.[self presentViewController:second animated:YES completion:nil];4.second.block = ^(NSString *str){5.self.nameLabel.text = str;6.};7.}在查閱資料的過程中,我還看到了以下幾種方案:(1)使用SharedApplication,定義一個變數來傳遞(感覺和單例的方式一樣)(2)使用文件,或者NSUserdefault來傳遞view sourceprint?01.//通過文件或者UserDefault方式存值(感覺不太適合此類傳值,如果要用文件或者UserDefault方式存值的話,可以考慮此方式)02.- (IBAction)userDefaultMethod:(id)sender {03.if([self notEmpty]) {04.[[NSUserDefaults standardUserDefaults] setObject:self.nameTextField.text forKey:@"myNameText"];05.[self dismissViewControllerAnimated:YES completion:nil];06.}else{07.[self showAlert];08.}09.}在A試圖控制器顯示view sourceprint?01.-(void)viewDidAppear:(BOOL)animated{02.[superviewDidAppear:animated];03.//如果想測試通過UserDefault方式傳值或者通過單例方式傳值,取消以下注釋即可04./*05.if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"myNameText"] length] != 0) {06.self.nameLabel.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"myNameText"];07.[[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"myNameText"];08.}09.DataSource *dataSource = [DataSource sharedDataSource];10.if ([dataSource.myName length] != 0) {11.self.nameLabel.text = dataSource.myName;12.dataSource.myName = @"";13.}14.*/15.}(3)通過一個單例的class來傳遞B試圖控制器view sourceprint?01.//通過單例方式傳值(感覺不太適合此類傳值,如果要用單例方式傳值的話,可以考慮此方式)02.- (IBAction)singletonMethod:(id)sender {03.if([self notEmpty]) {04.DataSource *dataSource = [DataSource sharedDataSource];05.dataSource.myName = self.nameTextField.text;06.[self dismissViewControllerAnimated:YES completion:nil];07.}else{08.[self showAlert];09.}10.}A試圖控制器顯示view sourceprint?01.-(void)viewDidAppear:(BOOL)animated{02.[superviewDidAppear:animated];03.//如果想測試通過UserDefault方式傳值或者通過單例方式傳值,取消以下注釋即可04./*05.if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"myNameText"] length] != 0) {06.self.nameLabel.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"myNameText"];07.[[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"myNameText"];08.}09.DataSource *dataSource = [DataSource sharedDataSource];10.if ([dataSource.myName length] != 0) {11.self.nameLabel.text = dataSource.myName;12.dataSource.myName = @"";13.}14.*/15.}16.@end這裡面用到了單例模式,編寫了DataSource這個類,存放數據view sourceprint?01.//02.// DataSource.h03.// TestCallBack04.//05.// Created by csdc-iMac on 14-7-17.06.// Copyright (c) 2014年 JuneWang. All rights reserved.07.//08.09.#import<Foundation/Foundation.h>10.11.@interfaceDataSource : NSObject12.@property(nonatomic, strong) NSString *myName;13.+(DataSource*)sharedDataSource;14.@endview sourceprint?01.//02.// DataSource.m03.// TestCallBack04.//05.// Created by csdc-iMac on 14-7-17.06.// Copyright (c) 2014年 JuneWang. All rights reserved.07.//08.09.#import"DataSource.h"10.11.@implementationDataSource12.+(DataSource *)sharedDataSource{13.staticDataSource *dataSource = nil;14.staticdispatch_once_t once;15.dispatch_once(&once, ^{16.dataSource = [DataSourcenew];17.});18.returndataSource;19.}20.@end程序運行截圖A視圖:

B視圖

當輸入姓名,並點擊對應的確認按鈕後,會回到A視圖,並顯示在B視圖中輸入的姓名

祝:玩得開心,有什麼別的辦法或者不正確的地方,歡迎指正。如果寫得不詳細,可以通過源碼分析。參考:http://blog.csdn.net/cocoarannie/article/details/11857141http://www.cnblogs.com/heri/archive/2013/03/18/2965815.html源碼地址:https://github.com/wangtao169447/PassValue出處:http://www.cnblogs.com/JuneWang/p/3850859.html
推薦閱讀:

洗澡方式看哪種女人最易出軌(圖)
12星座的"壁咚"方式,霸道強勢OR羞於言表...
五行命理,你適合哪種求財方式?
第四節  法治:現代社會治理的基本方式
牛仔褲5種搭配方式,時髦又好看

TAG:方式 | 頁面 |