0

Noob at iOS development. This is simply a feasibility question. I wanted to see if it was possible to have a TableViewController and, based upon the type of data that is pushed into the cell, have it do a pushViewController between two different controllers.

Like say for example, we have locations and people that can be returned as search results and we either want to do a pushViewController with either a ProfileController or a LocationController? Would it matter if one of the controllers were a UIWebView?

thx

EDIT 1 I guess I'm looking for a way to say if a TableView cell responds to a particular message similar to this question In Objective-C, given an id, how can I tell what type of object it points to? and determine which UIViewController to push.

Community
  • 1
  • 1
timpone
  • 19,235
  • 36
  • 121
  • 211

1 Answers1

0

If you want to "push" another review controller, your table view controller should be embedded in a UINavigationController. Then, there's two ways to handle pushing a new view controller when a row is selected:

Without storyboards (iOS < 5.0), your best opportunity to do things is when -tableView:didSelectRowAtIndexPath: is called on your table view's delegate (typically the table view controller). From there, you can choose where to go based on the indexPath that was passed in, create an instance of the view controller you want to go to, and push it onto the navigation controller. For example:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UIViewController *nextVC;
    if (indexPath.section == 0) {
        nextVC = [[ProfileController alloc] init];
        // configure nextVC as needed
    } else {
        nextVC = [[LocationController alloc] init];
        // configure nextVC as needed
    }
    [self.navigationController pushViewController:nextVC animated:YES];
}

With storyboards (iOS 5.0+), the normal way to do things is to drag a segue in IB from the table cell to the next view controller, and implement your logic to configure the destination view controller based on the selection in -prepareForSegue:sender:... but you can't drag multiple segues from the same table cell. Instead, drag segues from the view controller itself, and give them identifiers. Make your choice in -tableView:didSelectRowAtIndexPath: as before, but call performSegueWithIdentifier: instead of creating the destination view controller yourself. For example:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        [self peformSegueWithIdentifier:@"showProfile"];
    } else {
        [self peformSegueWithIdentifier:@"showLocation"];
    }
}

If you need to configure the destination view controller based on which table row was tapped, do that in -prepareForSegue:sender:.

There's no UIWebViewController... but this would be the same regardless of whether one of your view controllers managed a UIWebView.

rickster
  • 124,678
  • 26
  • 272
  • 326