如何传递prepareForSegue:一个对象
我在地图视图中有很多注释(带有 rightCalloutAccessory
按钮)。该按钮将执行从该mapview
到tableview
的segue。我想根据单击的标注按钮向 tableview
传递一个不同的对象(保存数据)。
例如:(完全编造)
- annotation1 (Austin) ->传递数据 obj 1(与奥斯汀相关)
- 注释 2(达拉斯)->传递数据 obj 2(与达拉斯相关)
- annotation3(休斯顿)->传递数据 obj 3 等等...(你得到 想法)
我能够检测到单击了哪个标注按钮。
我正在使用 prepareForSegue
: 将数据 obj 传递到目标 ViewController
。由于我无法使此调用为我需要的数据对象获取额外的参数,因此有哪些优雅的方法可以实现相同的效果(动态数据对象)?
任何提示将不胜感激。
I have many annotations in a mapview (with rightCalloutAccessory
buttons). The button will perform a segue from this mapview
to a tableview
. I want to pass the tableview
a different object (that holds data) depending on which callout button was clicked.
For example: (totally made up)
- annotation1 (Austin) -> pass data obj 1 (relevant to Austin)
- annotation2 (Dallas) -> pass data obj 2 (relevant to Dallas)
- annotation3 (Houston) -> pass data obj 3 and so on... (you get the
idea)
I am able to detect which callout button was clicked.
I'm using prepareForSegue
: to pass the data obj to the destination ViewController
. Since I cannot make this call take an extra argument for the data obj I require, what are some elegant ways to achieve the same effect (dynamic data obj)?
Any tip would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
只需在
prepareForSegue:
方法中获取对目标视图控制器的引用,并将所需的任何对象传递到那里即可。这是一个示例...修订:您还可以使用
performSegueWithIdentifier:sender:
方法根据选择或按下按钮激活到新视图的转换。例如,考虑我有两个视图控制器。第一个包含三个按钮,第二个需要知道在转换之前按下了哪些按钮。您可以将按钮连接到代码中的
IBAction
,该代码使用performSegueWithIdentifier:
方法,如下所示...编辑:我最初附加的演示应用程序现在已有六年历史,所以我将其删除以避免任何混淆。
Simply grab a reference to the target view controller in
prepareForSegue:
method and pass any objects you need to there. Here's an example...REVISION: You can also use
performSegueWithIdentifier:sender:
method to activate the transition to a new view based on a selection or button press.For instance, consider I had two view controllers. The first contains three buttons and the second needs to know which of those buttons has been pressed before the transition. You could wire the buttons up to an
IBAction
in your code which usesperformSegueWithIdentifier:
method, like this...EDIT: The demo application I originally attached is now six years old, so I've removed it to avoid any confusion.
有时,避免在两个视图控制器之间创建编译时依赖关系很有帮助。以下是您如何在不关心目标视图控制器的类型的情况下执行此操作:
因此,只要您的目标视图控制器声明一个公共属性,例如:
您可以按照我上面的描述在先前的视图控制器中设置此属性。
Sometimes it is helpful to avoid creating a compile-time dependency between two view controllers. Here's how you can do it without caring about the type of the destination view controller:
So as long as your destination view controller declares a public property, e.g.:
you can set this property in the previous view controller as I described above.
在 Swift 4.2 中我会做类似的事情:
In Swift 4.2 I would do something like that:
我有一个 sender 类,就像这样,
我使用这个 sender 类 将对象传递给
prepareForSeque:sender:
I have a sender class, like this
I use this sender class for passing objects to
prepareForSeque:sender:
当我试图学习如何将数据从一个视图控制器传递到另一个视图控制器时,我遇到了这个问题。不过,我需要一些视觉效果来帮助我学习,所以这个答案是对这里已有的其他答案的补充。它比原来的问题更笼统一些,但可以适应工作。
这个基本示例的工作原理如下:
这个想法是将字符串从第一个视图控制器中的文本字段传递到第二个视图控制器中的标签。
第一个视图控制器
第二个视图控制器
请记住
UITextField
和UILabel
的插座。来源
如何通过 segue (swift) 发送数据(YouTube 教程)
另请参阅
视图控制器:向前传递数据和向后传递数据(更完整的答案)
I came across this question when I was trying to learn how to pass data from one View Controller to another. I need something visual to help me learn though, so this answer is a supplement to the others already here. It is a little more general than the original question but it can be adapted to work.
This basic example works like this:
The idea is to pass a string from the text field in the First View Controller to the label in the Second View Controller.
First View Controller
Second View Controller
Remember to
UITextField
and theUILabel
.Source
How to send data through segue (swift) (YouTube tutorial)
See also
View Controllers: Passing data forward and passing data back (fuller answer)
对于 Swift 使用这个,
For Swift use this,
我在 UIViewController 上实现了一个带有类别的库,可以简化此操作。
基本上,您可以在与执行转场的 UI 项关联的 NSDictionary 中设置要传递的参数。它也适用于手动 Segue。
例如,您可以执行
手动转场或创建带有转场的按钮并使用
如果目标视图控制器不符合键的键值编码,则不会发生任何情况。它也适用于键值(对于展开转场很有用)。
在这里查看
https://github.com/stefanomondino/SMQuickSegue
I've implemented a library with a category on UIViewController that simplifies this operation.
Basically, you set the parameters you want to pass over in a NSDictionary associated to the UI item that is performing the segue. It works with manual segues too.
For example, you can do
for a manual segue or create a button with a segue and use
If destination view controller is not key-value coding compliant for a key, nothing happens. It works with key-values too (useful for unwind segues).
Check it out here
https://github.com/stefanomondino/SMQuickSegue
我的解决方案是类似的。
My solution is similar.
只需使用这个功能即可。
Just use this function.
我使用了这个解决方案,这样我就可以在同一个函数中保留对 segue 的调用和数据通信:
用例如下:
这似乎对我有用,但是,我不能 100% 确定执行和准备函数总是在同一个线程上执行。
I used this solution so that I could keep the invocation of the segue and the data communication within the same function:
A use case would be something like:
This seems to work for me, however, I'm not 100% sure that the perform and prepare functions are always executed on the same thread.