如何将字典从一个viewcontroller传递到另一个viewcontroller

时间:2014-03-28 11:30:09

标签: objective-c ios7 xcode5 uistoryboard

我有两个使用segue连接的视图控制器。如何将第二个视图控制器中的一个字典{"拳击":1,"第二个":2}传递给第一个视图控制器。 / p>

1 个答案:

答案 0 :(得分:0)

您必须在故事板中为segue添加标识符。 在源视图中,控制器覆盖prepareForSegue:方法:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.identifier isEqualToString:@"YOURIDENTIFIER"]) { //<-YOURIDENTIFIER the same as you set up in storyboard

        UIViewController *vc = (UIViewController*)segue.destinationViewController;
        // You should cast destination view controller to your custom view controller sub class:
       //YourViewController *vc = (YourViewController*)segue.destinationViewController;
       // Pass the data
       vc.myDictionary = @{{"fist":1,"second":2};
       // I assume you have property myDictionary in YourViewController class
    }
}

希望这个帮助