How do I create a UIViewController programmatically?
Solution 1
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.
Solution 2
UIViewController
s 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).
pankaj
Updated on July 09, 2022Comments
-
pankaj almost 2 years
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 aUITableView
. 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. -
pankaj about 14 yearsThanks eman for answer, in this new view controller i need to display a UITableview with records. How will i do that?
-
pankaj about 14 yearsRight now i am on a viewcontroller and want to direct the user to new viewcontroller and display data in a tableview
-
pankaj about 14 yearsand yes, one more thing user can also further drill down to next level from this new programmically created viewcontroller
-
pankaj about 14 yearsand yes, one more thing user can also further drill down to next level from this new programmically created viewcontroller
-
shosti about 14 years(updated example) In this example, each table view controller would have an array of records, each of which would have an array of sub-records (correct me if I'm not understanding the design correctly). So you could have a class called
Record
, which would have a name (displayed on the table cell) and anNSArray
ofRecord
s--each table view controller would display the array of sub-records. -
Rémi Vennereau about 14 yearsThen you probably want your first view controller to be a UINavigationController.
-
pankaj about 14 yearsaccording to ur last comment i will have have to bring all rows from my database and save it. It is not my possible as that way i will be ended up saving hundreds of thousands of rows and only using only few of them
-
shosti about 14 years@pankaj: it's hard to know your desired design based on your description. Can you post more specifics? (Are you using Core Data?)
-
pankaj about 14 yearsok, i m having a tableview where i am showing some data fetched from web services. There could be thousands of rows in the database but i want to display them in a drill down way as there is some relation in them. Now on first page i display main categories. When user selects any one of them it will display the further sub categories. I dont know upto how many levels i will be getting the records, so i cant have a fixed number of viewcontrollers. So i trying to generate them programmically. When user selects a row i want to create a view controller, display it with tableview and records....
-
pankaj about 14 years.... and when user selects any of the row in that table view(created dynamically) it should again be redirected to new view controller(dynamically created). I hope i am more clear now
-
wcochran over 10 yearsIts actually better to set the view lazily by overriding
loadView
when a controller is created programatically. -
Phani Sai about 8 yearsuse following link for how to add child view in viewController stackoverflow.com/questions/27151026/…