10

I'm starting now with Xcode on 4.2 for iOS5 and there are a few changes and I'm now crossing a problem that I can't figure out a way to solve it.

I'm doing an example with a UITablwView that is populated programmatically with 2 Sections, 1st section with only 1 Row, and 2nd Section with 3 Rows.

My aim is to select a row from the table, and based on that row, the user will be redirected to different Views.

For example: selecting section 0 row 0, app pushes to view 1 - name setting // selecting section 1 row 0, app pushes to view 3 - address setting

The old fashion way, this is quite simple, just needed to init a UIViewController with initWithNibName and then push the view.

Now with the storyBoard everything changes, or at least I think it changes because I can't see how to get the same result since I can't set multiple segue's from the tableView to different UIViewControllers...and to do the old fashion way I can't see where I can get the NIB names from the views on the storyBoard to init an UIViewController to push.

Does any one knows how to get to this result??

4

3 回答 3

25

在故事板中从源视图控制器定义两个“通用”segue(例如标识为“segue1”和“segue2”),一个到每个目标视图控制器。这些 segue 不会与任何操作相关联。

然后,有条件地执行你的 segues 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];
    }
}
于 2012-01-15T07:02:06.477 回答
16

我和你有同样的问题。问题是您无法将tableViewCell链接到多个视图控制器。但是,您可以将源视图本身链接到多个视图控制器。

  1. 按住 Control 键将主视图控制器(而不是表视图单元格)从场景查看器拖动到您要链接的任何视图控制器。您可以随心所欲地执行此操作。请注意,源视图控制器场景中显示的 segue 应该类似于“Push Segue from Root View Controller ...”而不是“Push Segue from NavCell to ...”。

  2. 识别每个 segue 链接的唯一名称,例如“toDetailView1”

  3. 最后,在源视图控制器中自定义选择:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (indexPath.row % 2 == 1) {
            [self performSegueWithIdentifier:@"toDetailView1" sender:self];
        } else {
            [self performSegueWithIdentifier:@"toDetailView2" sender:self];
        }
    }
    
于 2012-05-30T03:21:32.903 回答
0

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];
}
于 2014-07-25T11:04:44.580 回答