performSegueWithIdentifier and sharedData(performSegueWithIdentifier 和 sharedData)
问题描述
我希望我问的不是已经回答的问题(但我没有找到答案,所以希望我没有).
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:performSegueWithIdentifier 和 sharedData
基础教程推荐
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01