Subclass UIViewController to create a list programmatically without using a nib
All I currently need is to create a tableview programmatically by subclassing UIViewController and applying the necessary methods from without using any nib file. It will probably need a UINavigationViewControlloer. I basically understand all these concepts and even almost know how to piece everything together but something is missing. It will have 4 files. AppDelegate h/m and SomeNameViewcontroller h/m. I think the Som开发者_StackOverflow中文版eNameViewController should have a property UITableView*
I've tried several ways and looked online but i keep getting a black screen with a navigationbar. It's incredibly frustrating and when i can get the tableView to show, I can't get the tableView to reload data :/
From your narrative, it seems that you do not fully grasp the concepts involved. For example, there is no such thing as a UINavigationViewController
. Here is what you need to know:
- A
UINavigationController
is a controller of view controllers.
You can create it in your app delegate with
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:
yourRootViewController];
and access it from the view controller with self.navigationController
.
For your purposes, you create a subclass of a
UITableViewController
. The template in Xcode should already have stubs of all thedatasource
anddelegate
methods you need.In your app delegate, make sure you have assigned the right navigation controller as the root.
In application:didFinishLaunchingWithOptions:
:
self.window.rootViewController = nav; // the nav controller you created
[nav release];
[self.window makeKeyAndVisible];
return YES;
Sounds like you only need a UITableVIewController
. And like Mundi mentioned, xcode takes care of this very well. Here is what you have to do:
Create a new project in XCode and choose the
Window-based Application
template.- This gives you the basic stuff, mainly a AppDelegate.
- This gives you the basic stuff, mainly a AppDelegate.
Add a new file that is a
UIViewController subclass
. When given the option change the subclassing to UITableViewController.- After that you will be given the option to use a nib file, uncheck that.
- This will give you a UITableViewController with a UITableView already wired (
Delegate
andDataSource
) to that controller.
Create your
NavigationController
in theAppDelegate
and push yourTableViewContoller
.[self.navigationController pushViewController:yourTableViewController animated:NO];
Setup your
NavigationContoller
as theRootController
like Mundi described.
Sections 6 and 7 of the Stanford Developing Apps for iOS explain NavigationControllers
and other controllers (e.g. TabBarController
) very well.
精彩评论