iOS:切換視圖時,反向傳遞資料方法一:通知

來源:互聯網
上載者:User

標籤:

通知方式:

     1.有一個(單例)通知中樞,負責管理iOS中的所有通知

     2.需要擷取某種通知,必須註冊成為觀察者(訂閱)

     3.不再需要取某種通知時,要取消註冊。

     4.你可以向通知中樞發送某種通知,通知中樞會轉寄給相應的觀察者(訂閱者)。

 

將第一個控制器和第二個控制器以modal方式聯結後,每一個控制器和各自的類相關聯,同時將segue的idetifier標識設定一個名字,正向傳資料時,需要根據segue的標識符進行唯一的識別。反向傳資料時,採用通知的方法。

1、所有的檔案:

2、第一個控制器FirstViewController關聯的類為:

3、第二個控制器SecondViewcontroller關聯的類為:

4、給segue的identifier設定一個名字,作為標識

具體代碼如下:

FirstViewController控制器關聯的viewController(.h/.m)類:

 1 #import "ViewController.h" 2 #import "SecondViewController.h" 3  4 @interface ViewController () 5 @property (weak, nonatomic) IBOutlet UITextField *firstTextField; 6  7 @end 8  9 @implementation ViewController10 11 - (void)viewDidLoad {12     [super viewDidLoad];13 }14 15 //重寫該方法,視圖切換時,自動調用16 -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender17 {18     if([segue.identifier isEqualToString:@"modal"])19     {20         //擷取目的控制器21         SecondViewController *secondVC = segue.destinationViewController;22         23         //正向傳資料24         secondVC.information = self.firstTextField.text;25         26         //註冊通知,成為觀察者27         [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receiveInfo:) name:NOTIFICATIONTEPY object:nil];28     }29 }30 31 //receiveInfo事件32 -(void)receiveInfo:(NSNotification*)notification33 {34     //反向接收通知中的資料35     self.firstTextField.text = [notification.userInfo objectForKey:NOTIFICATIONINFOKEY];36     37     //取消註冊38     [[NSNotificationCenter defaultCenter]removeObserver:self name:NOTIFICATIONTEPY object:nil];39 }40 @end

 

SecondViewController控制器關聯的SecondViewController(.h/.m)類:

 1 #import "SecondViewController.h" 2  3 @interface SecondViewController () 4 @property (weak, nonatomic) IBOutlet UITextField *secondTextField; 5  6 @end 7  8 @implementation SecondViewController 9 //返回時的觸發事件10 - (IBAction)returnClicked:(UIBarButtonItem *)sender11 {12     //反向傳遞資料13     14     //1、訊息內容15     NSDictionary *dicInfo = @{NOTIFICATIONINFOKEY:self.secondTextField.text};16     17     //2、通過通知中樞傳遞出去18     [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATIONTEPY object:self userInfo:dicInfo];19     20     //關閉模態視窗21     [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];22     //[self dismissViewControllerAnimated:YES completion:nil];23 }24 25 - (void)viewDidLoad {26     [super viewDidLoad];27     28     // 顯示文字框內容(接受傳遞過來的資料)29     self.secondTextField.text = self.information;30 }31 32 @end

 

iOS:切換視圖時,反向傳遞資料方法一:通知

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.