問題描述
我在故事板中有兩個控制器,嵌入在 NavigationController 中,并且有一個 segue 可以在它們之間切換.
I have two controllers in the storyboard, embedded in a NavigationController, and there is a segue to switch between these.
通過實現 prepareForSegue 將數據從第一個控制器傳遞到第二個控制器非常簡單,并使用 segue.destinationViewController 設置第二個控制器的屬性.
Passing data from the first controller to the second one is pretty straightforward by implementing prepareForSegue, and set the properties of the second controller using segue.destinationViewController.
我也應該將數據從第二個控制器傳回到前一個控制器.我用谷歌搜索,但我沒有找到任何簡單但有效的代碼來演示它.
I should pass back data to the from the second controller to the previous one also. I googled, but I have not found any simple, but working code to demonstrate it.
您能不能給我一個簡單的示例,說明最好的方法?
Would you be so kind give me a simple sample about the best way to do it?
提前致謝!
推薦答案
在您的第二個視圖控制器類中,您創建一個協議和委托.第一個視圖控制器將自己設置為 prepareForSegue 中的委托并實現協議方法.然后,第二個視圖控制器將調用方法將數據傳回第一個視圖控制器.以下是我的一個項目中的一些代碼作為示例.
In your second view controller class you create a protocol and delegate. The first view controller will set it self as the delegate in prepareForSegue and implement the protocol methods. The second view controller will then call the methods to pass data back to the first view controller. Here is some code from one of my projects as an example.
@protocol TableSelectorDelegate <NSObject>
@optional
- (void)didMakeSelection:(id)selectionString forType:(NSString *)dataTitle;
- (void)didAddNewValue:(NSString *)newValue forType:(NSString *)dataTitle;
@end
@interface TableSelectorViewController : UITableViewController
@property (nonatomic, weak) id<TableSelectorDelegate> delegate;
@end
這篇關于將數據傳回前一個控制器的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!