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

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

问题描述

我在地图视图中有很多注释(带有 rightCalloutAccessory 按钮).该按钮将执行从此 mapviewtableview 的转场.我想根据点击的标注按钮向 tableview 传递一个不同的对象(保存数据).

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 (Austin) -> 传递数据 obj 1(与 Austin 相关)
  • annotation2(达拉斯)-> 传递数据obj 2(与达拉斯相关)
  • annotation3 (Houston) -> 传递数据 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)?

任何提示将不胜感激.

推荐答案

只需在 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.

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

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];
    }
}

我最初附加的演示应用程序现在已有 6 年历史了,所以我已将其删除以避免任何混淆.

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

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

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