如何使用故事板导航不同的类

时间:2014-05-13 12:11:01

标签: ios objective-c storyboard uistoryboardsegue

如果用户点击按钮,使用故事板,它将根据随机数导航到不同的类,我无法准备3个segues所以请帮助我。

我的代码是

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    int randomNumber= arc4random() % 3;
    NSIndexPath *indexPath = [tableview indexPathForSelectedRow];

    if ([segue.identifier isEqualToString:@"randomsegue"]){
        if (randomNumber==0){
            //navigate to first view controller
        }
        else if(randomNumber==1){
            //navigate to second view controller
        }
        else{
            //navigate to third view controller
        }
    }
}

2 个答案:

答案 0 :(得分:1)

检查一下,我认为你想要的是这个。

-(IBAction)buttonAction:(id)sender{

int randomNumber= arc4random() % 3;

switch (randomNumber) {
    case 0:
         [self performSegueWithIdentifier:@"segue1" sender:sender];
        break;
    case 1:
         [self performSegueWithIdentifier:@"segue2" sender:sender];
        break;
    case 2:
         [self performSegueWithIdentifier:@"segue3" sender:sender];
        break;
    default:
        break;
}

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if ([segue.identifier isEqualToString:@"segue1"])
{
    //navigate to view controller 1
}
else if ([segue.identifier isEqualToString:@"segue2"])
{

    //navigate to view controllr 2
}
else{

    //navigate to view controller 3
 }
}

答案 1 :(得分:0)

使用标识符设置所有可能的seques,然后在switch语句中设置带有相应标识符的IBAction。