pankaj
pankaj

Reputation: 8348

How do I create a UIViewController programmatically?

I am working in a app where i have data in UITableView. It is like a drill down application. User will click on a row and will go to next page showing more records in a UITableView. But problem in my case is that i dont know upto how many level user can drill. The number of levels are not fixed. So now i am thinking to create and add the viewcontrollers programmatically. Is it possible?? if yes how? thanks in advance.

Upvotes: 21

Views: 51526

Answers (2)

shosti
shosti

Reputation: 7422

UIViewControllers are always created programmatically. It sounds like you just need to have the same class for each level of view controller, e.g.:

//CoolViewController:UITableViewController
//CoolViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (!self.isAtTopLevel) {
        CoolViewController *cvc = [[CoolViewController alloc] initWithRecord:[self.records objectAtIndex:indexPath.row]];
        [self.navigationController pushViewController:cvc animated:YES];
        [cvc release];
    } else {
        //do something else
    }
}

In this case, thingies would be some sort of recursive NSArray (i.e. an array of arrays).

Upvotes: 4

willcodejavaforfood
willcodejavaforfood

Reputation: 44063

UIViewController *controller = [[UIViewController alloc] init];
controller.view = whateverViewYouHave;

Do you have your own view controller that you coded? In that case you probably don't need to set the view property as it has been set in IB if that is what you used. When you have your controller you can push it onto the navigationController or view it modally etc.

Upvotes: 23

Related Questions