只需在prepareForSegue:
method中获取对目标视图控制器的引用,然后将所需的任何对象传递到该对象即可。这是一个例子
- (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:
方法基于选择或按下按钮来激活到新视图的过渡。
例如,考虑我有两个视图控制器。第一个包含三个按钮,第二个需要知道在转换之前已按下了哪个按钮。您可以将按钮连接到IBAction
使用performSegueWithIdentifier:
方法的代码中的,例如...
// 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年的历史,因此为了避免混淆,我将其删除。