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

人到中年有點甜
TA貢獻1895條經(jīng)驗 獲得超7個贊
您可以使用代理人。因此,在ViewController B中,您需要創(chuàng)建一個將數(shù)據(jù)發(fā)送回ViewController A的協(xié)議。您的ViewController A將成為ViewController B的委托。
如果您不熟悉目標C,請查看什么是代表。
在ViewControllerB.h中創(chuàng)建協(xié)議:
#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中:當你轉(zhuǎn)到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}
編輯:
您可以查看此示例:如何將數(shù)據(jù)傳遞回上一個viewcontroller:Tutorial鏈接

慕森卡
TA貢獻1806條經(jīng)驗 獲得超8個贊
甲短和更簡單的比協(xié)議/委托方法是創(chuàng)建一個閉合:
用于在我的情況下發(fā)回一個字符串。在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)}
- 3 回答
- 0 關注
- 693 瀏覽
添加回答
舉報
0/150
提交
取消