使用performSegue将数据传递到目标视图控制器

时间:2015-06-25 01:02:35

标签: ios segue uistoryboardsegue xcode-storyboard

所以我知道如果有一个通过故事板设置和激活的segue,可以使用prepareForSegue方法将数据传递到目标视图控制器。

我想知道如果使用performSegueWithIdentifier方法,是否有办法做到这一点?似乎prepareForSegue方法仅在使用故事板激活segue时调用,而不是使用performSegue激活。

一个特别的问题是我无法访问UIStoryboardSegue(它不是performSegue中的参数,因为它在prepareForSegue中),因此无法访问目的地VC也是。

1 个答案:

答案 0 :(得分:0)

答案在StackOverflow中。 How to pass prepareForSegue: an object

// When any of 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];
    }
}