如何传递prepareForSegue:一个对象 [英] How to pass prepareForSegue: an object

查看:87
本文介绍了如何传递prepareForSegue:一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在mapview中有很多注释(使用 rightCalloutAccessory 按钮)。该按钮将执行从此 mapview tableview 的segue。我想传递 tableview 一个不同的对象(包含数据),具体取决于点击的是哪个callout按钮。

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.

例如:(完全组成)


  • annotation1(奥斯汀) - >传递数据obj 1(与奥斯汀相关)

  • annotation2(达拉斯) - >传递数据obj 2(与达拉斯相关)

  • annotation3(休斯顿) - >传递数据obj 3等等...(你得到
    的想法)

我能够检测到点击了哪个标注按钮。

I am able to detect which callout button was clicked.

我正在使用 prepareForSegue :将数据obj传递到目标 ViewController 。因为我无法进行此调用需要额外的参数来获取我需要的数据obj,有什么优雅的方法可以实现相同的效果(动态数据obj)?

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.

推荐答案

只需在 prepareForSegue中获取对目标视图控制器的引用:方法并将您需要的任何对象传递给那里。这是一个例子...

Simply grab a reference to the target view controller in prepareForSegue: method and pass any objects you need to there. Here's an example...

- (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
        YourViewController *vc = [segue destinationViewController];

        // Pass any objects to the view controller here, like...
        [vc setMyObjectHere:object];
    }
}

修订:你也可以使用 performSegueWithIdentifier:sender:根据选择或按下按钮激活转换到新视图的方法。

REVISION: You can also use performSegueWithIdentifier:sender: method to activate the transition to a new view based on a selection or button press.

例如,考虑我有两个视图控制器。第一个包含三个按钮,第二个按钮需要知道在转换之前按下了哪些按钮。您可以在代码中将按钮连接到 IBAction ,该代码使用 performSegueWithIdentifier:方法,就像这样...

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 uses performSegueWithIdentifier: method, like this...

// When any of my buttons are pressed, push the next view
- (IBAction)buttonPressed:(id)sender
{
    [self performSegueWithIdentifier:@"MySegue" sender:sender];
}

// This will get called too before the view appears
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"MySegue"]) {

        // Get destination view
        SecondView *vc = [segue destinationViewController];

        // Get button tag number (or do whatever you need to do here, based on your object
        NSInteger tagIndex = [(UIButton *)sender tag];

        // Pass the information to your destination view
        [vc setSelectedButton:tagIndex];
    }
}

编辑:我最初附加的演示应用程序现在已有六年了,所以我已将其删除以避免混淆。

The demo application I originally attached is now six years old, so I've removed it to avoid any confusion.

这篇关于如何传递prepareForSegue:一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆