我希望我问的不是已经回答的问题(但我没有找到答案,所以希望我没有).
I hope I'm not asking something that's been already answered (but I found no answer to this, so hopefully I'm not).
我在当前的 xcode 版本中有一个应用程序,使用 segues 和 navigationController.我需要将数据从一个视图传递到另一个视图 - 最简单的方法是什么?我遇到了一些可能与 performSegueWithIdentifier 方法挂钩的 sharedData 东西,但不知道如何使用它(或者这样做是否是正确的选择).
I have an app in the current xcode version, using segues and navigationController. I need to pass data from one view to the other - what's the easiest way to do this? I ran onto some sharedData thing that could be possibly hooked onto the performSegueWithIdentifier method but don't know how to use it (or whether it is the right choice to do it like this).
谢谢
一个segue有两个视图控制器:sourceViewController
和destinationViewController
.当 UIKit 执行 segue 时,它会向源 VC 发送 prepareForSegue:sender:
消息.您可以在视图控制器子类中重写该方法以将数据传递给目标 VC.
A segue has two view controllers: sourceViewController
and destinationViewController
. When UIKit executes a segue, it sends a prepareForSegue:sender:
message to the source VC. You can override that method in your view controller subclass to pass data to the destination VC.
例如,假设您有一个带有电影表视图的主视图控制器,并且当用户单击表视图中的一行时,您希望转到电影的详细视图控制器.
For example, suppose you have a master view controller with a table view of movies, and when the user clicks a row in the table view, you want to segue to a detail view controller for the movie.
@implementation MasterViewController
...
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
DetailViewController *detailVC = segue.destinationViewController;
NSIndexPath *selectedPath = [self.tableView indexPathForSelectedRow];
detailVC.movie = [self movieForIndexPath:selectedPath];
}
Interface Builder Storyboard 简介中对此进行了解释来自 WWDC 2011 的视频.
This is explained in the Introducing Interface Builder Storyboarding video from WWDC 2011.
另外值得注意的是,当segue的来源是table view cell,或者table view cell的附属按钮时,prepareForSegue:sender:
的sender
参数是表格视图单元格.
It's also worth noting that when the segue's origin is a table view cell, or the accessory button of a table view cell, the sender
argument of prepareForSegue:sender:
is the table view cell.
这篇关于performSegueWithIdentifier 和 sharedData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!