问题描述
这是我的故事板的场景.
This is the scene of my storyboard.
UINavigationController
-> UITableViewController
-> push segue -> UIViewController
UINavigationController
-> UITableViewController
-> push segue -> UIViewController
我知道如何将UITableViewController
的数据传递给UIViewController
.但是,如何将数据传回 UITableViewController
?(UIViewController
有一个带有后退按钮的导航栏)
I know how to pass the data of UITableViewController
to UIViewController
. But, how can I pass the data back to the UITableViewController
?(UIViewController
has a navigation bar with a back button)
推荐答案
您可以通过使用委托来完成.
You can do it through the use of delegation.
当您将UITableViewController
的数据传递给UIViewController
时,您可以在UIViewController
上设置一个委托.每当你想将数据传回UITableViewController
时,你可以调用delegate并传入数据
When you pass data of UITableViewController
to UIViewController
, you can set a delegate on UIViewController
. Whenever you want to pass data back to UITableViewController
, you can call the delegate and pass in the data
这是一个更具体的代码示例
Here's a more concrete example in code
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
{
// Get reference to the destination view controller
UIViewController *vc = [segue destinationViewController];
// Pass the UITableViewController to the UIViewController
[vc setDelegate:self];
}
}
在 UITableViewController
上创建一个从 UIViewController
获取数据的方法(示例:-(void)processDataFromUIViewController:(NSString *)string
)
Create a method on UITableViewController
that takes data from UIViewController
(example: -(void)processDataFromUIViewController:(NSString *)string
)
现在当你想把数据传回UITableViewController
时,调用[delegate sendDataFromUIViewController:@"data"]
Now when you want to pass data back to UITableViewController
, call [delegate sendDataFromUIViewController:@"data"]
这篇关于如何传回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!