问题描述
我在故事板中有两个控制器,嵌入在 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
这篇关于将数据传回前一个控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!