开发者

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 the datasource and delegate 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:

  1. Create a new project in XCode and choose the Window-based Application template.

    • This gives you the basic stuff, mainly a AppDelegate.

  2. 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.

  3. Create your NavigationController in the AppDelegate and push your TableViewContoller.

    • [self.navigationController pushViewController:yourTableViewController animated:NO];
  4. Setup your NavigationContoller as the RootController like Mundi described.

Sections 6 and 7 of the Stanford Developing Apps for iOS explain NavigationControllers and other controllers (e.g. TabBarController) very well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜