Xcode 4.2 iOS 5 : Multiple segues from a UITableView

别来无恙 提交于 2019-11-30 07:03:40

Define two "generic" segues (identified as "segue1" and "segue2", for example) in the storyboard from your source view controller, one to each destination view controller. These segues won't be associated with any action.

Then, conditionally perform the segues in your UITableViewDelegate:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Conditionally perform segues, here is an example:
    if (indexPath.row == 0)
    {
        [self performSegueWithIdentifier:@"segue1" sender:self];
    }
    else
    {
        [self performSegueWithIdentifier:@"segue2" sender:self];
    }
}

I have the same problem as you do. The problem is that you can't link your tableViewCell to multiple view controllers. However you can link your source view itself to multiple view controllers.

  1. Control-drag the master view controller (instead of table view cell) from the scene viewer to whatever view controller you want to link. You can do this as much as you want. Notice that the segue shown in source view controller scene should be something like "Push Segue from Root View Controller ..." instead of "Push Segue from NavCell to ...".

  2. Identify each segue link a unique name like "toDetailView1"

  3. Finally, custom the selection in your source view controllers:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (indexPath.row % 2 == 1) {
            [self performSegueWithIdentifier:@"toDetailView1" sender:self];
        } else {
            [self performSegueWithIdentifier:@"toDetailView2" sender:self];
        }
    }
    

Like @陳仁乾 and @Marco explained was completely correct. To make everything a little bit easier I would recommend you to use a single NSArray which will be initialized when viewDidLoad. Just name the segues the same as your UIViewControllers, this way you can Display a correct description of what UIViewControllers you can choose from and you can also perform the segues from this NSArray:

(Actually I'm not sure if it can cause any problems calling the segue the same as the UIViewController you want to call. Please let me know if this is BadPractise)

viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];

    _arraySessions = [[NSArray alloc] initWithObjects:
                      @"MyViewControllerName", nil];
}

cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = 
     [tableView dequeueReusableCellWithIdentifier:@"overviewCell"
                                     forIndexPath:indexPath];

    [cell.textLabel setText:_arraySessions[indexPath.row]];

    return cell;
}

didSelectRowAtIndexPath

- (void)tableView:(UITableView *)tableView 
        didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self performSegueWithIdentifier:_arraySessions[indexPath.row] 
                              sender:self];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!