亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將數據傳遞回上一個viewcontroller

將數據傳遞回上一個viewcontroller

開滿天機 2019-08-06 17:23:19
將數據傳遞回上一個viewcontroller我正在嘗試將數據傳遞回以前的viewController。有誰知道如何將數據從ViewController B傳遞回ViewController A?所以我想要一個字符串'從'BIDAddTypeOfDealViewController轉到BIDDCCreateViewController。用戶編輯了viewController B,我想在ViewController A中返回已編輯的數據然后我使用它。我正在使用此答案的“傳遞數據”部分。我的不同之處:第3點和第6點只是在彈出視圖時提及,所以我將該代碼放在viewWillDisappear中。我認為這是正確的嗎?同樣在Point 6,我沒有使用nib進行初始化,因為它已經過時了。我正在使用故事板。而且我沒有添加最后一行,因為我不相信我會推動它。按我的故事板上的按鈕已經讓我前進了。我認為問題可能出現在BIDDCCreateViewController中,我有方法,但我無法運行它。要運行一個方法,它應該[自我方法]。我無法做到這一點。那就是我猜的。它編譯并運行良好,沒有任何記錄,所以我不知道它是否有效。
查看完整描述

3 回答

?
人到中年有點甜

TA貢獻1895條經驗 獲得超7個贊

您可以使用代理人。因此,在ViewController B中,您需要創建一個將數據發送回ViewController A的協議。您的ViewController A將成為ViewController B的委托。

如果您不熟悉目標C,請查看什么是代表。

在ViewControllerB.h中創建協議:

#import <UIKit/UIKit.h>@protocol senddataProtocol <NSObject>-(void)sendDataToA:(NSArray *)array; //I am thinking my data is NSArray, you can use another object for store your information. @end@interface ViewControllerB : UIViewController@property(nonatomic,assign)id delegate;

ViewControllerB.m

@synthesize delegate;-(void)viewWillDisappear:(BOOL)animated{
     [delegate sendDataToA:yourdata];}

在ViewControllerA中:當你轉到ViewControllerB時

ViewControllerA *acontollerobject=[[ViewControllerA alloc] initWithNibName:@"ViewControllerA" bundle:nil];acontollerobject.delegate=self; // protocol listener[self.navigationController pushViewController:acontollerobject animated:YES];

并定義你的功能:

-(void)sendDataToA:(NSArray *)array{
   // data will come here inside of ViewControllerA}

編輯:

您可以查看此示例:如何將數據傳遞回上一個viewcontroller:Tutorial鏈接


查看完整回答
反對 回復 2019-08-06
?
慕森卡

TA貢獻1806條經驗 獲得超8個贊

短和更簡單的比協議/委托方法是創建一個閉合:

用于在我的情況下發回一個字符串。在ViewControllerA中:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let viewControllerB = segue.destination as? ViewControllerB {
        viewControllerB.callback = { message in            //Do what you want in here!
        }
    }}

在ViewControllerB中:

var callback : ((String) -> Void)?@IBAction func done(sender: AnyObject) {
    callback?("Hi")
    self.dismiss(animated: true, completion: nil)}


查看完整回答
反對 回復 2019-08-06
  • 3 回答
  • 0 關注
  • 700 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號