Kevin
Kevin

Reputation: 1479

iPhone - Load a new UIView on pressing button

say I have a UITableView and upon selecting a new table row (thus firing the delegate method), I want to load a new UIView (which I have created in storyboard) and pass to it a data item.

Obviously the new UIView is added as a UIViewController in storyboard and has its own controller class..

How would I go about doing that?

Thank you!

Upvotes: 1

Views: 294

Answers (2)

Sebastian Flückiger
Sebastian Flückiger

Reputation: 5555

Try something along the lines of this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:[NSBundle mainBundle]];
    [self.navigationController pushViewController:dvController animated:YES];
}

Upvotes: 2

Akash Malhotra
Akash Malhotra

Reputation: 1106

If you are using storyboards and not XIB files, use this code...

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailViewController *dvController = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];

    [self.navigationController pushViewController:dvController animated:YES];
}

Also you need to set the "Identifier" field in Interface Builder to the name used in self.storyboard instantiateViewControllerWithIdentifier:

Cheers!

Upvotes: 1

Related Questions