Does anyone know of a tutorial, or an app with sample code that shows how one might include a table drill down, but not right at the beginning of the app. The only tutorials i can find demonstrate it right as the user opens the app, but i would like to see it where the user navigates to t开发者_JAVA百科he table and then drills down. Thanks!
Well, no tutorials off the top of my head, but I believe the stock Navigation Based Application template creates a project where a table view is the root view controller of a navigation controller which is just like the tutorials you have mentioned except without any subviews to navigate to. To change this so that the app first loads a blank view with maybe a button that then goes to the table view just follow these directions after starting up a new project with the Navigation Based Application template. Remember that my directions here stick as close as possible to what's already being done by the Navigation Based Application template and makes maximal use of Interface Builder rather than coding views by hand:
1) Go into the .m of the default RootViewController, right click and refactor it to something like "SimpleTableViewController"
2) Well guess what...Xcode4 probably renamed the .m & .h files correctly, and renamed the contents of the .xib, but didn't rename the name of the .xib itself so it should still be called RootViewController.xib. If so, just manually rename the RootViewController.xib to SimpleTableViewController.xib.
2) Go to File->New File->UIViewController subclass, and save this as the 'new' "RootViewController"
3) Open RootViewController.xib and drag and drop a Round Rect Button from the Objects Library in the Utilities section on the right.
4) Rename the Round Rect Button to something like "Go to Simple Table View"
6) Add this method definition to RootViewController.h (just before @end)
- (IBAction) goToSimpleTableView;
7) Add the method body to RootViewController.m (just after @implementation):
- (IBAction) goToSimpleTableView
{
SimpleTableViewController* simpleTableViewController = [[SimpleTableViewController alloc] init];
MyAppDelegate* appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate.navigationController pushViewController:simpleTableViewController animated:YES];
[simpleTableViewController release];
}
Also make sure to add #import "MyAppDelegate.h"
and #import "SimpleTableViewController.h"
after the first #import
statement. (Here I'm assuming a project name of MyApp which would create a MyAppDelegate.h. Replace 'MyApp' with the actual name of your application delegate).
8) Next, go to MainWindow.xib, click on the SimpleTableViewController object under the Navigation Controller, open the identity inspector on the right, and change its class to RootViewController.
9) Now when you run the app, you'll see the new RootViewController with the button you added as the first view.
10) Next make sure to bind the 'Touch Up Inside' Send Event of the button in the RootViewController to the -goToSimpleTableView method by right clicking the button, clicking and dragging from the touch up inside node in the menu that pops up to File's Owner and then selecting the -goToSimpleTableView method.
11) Run the app again, and now when you click the button it should take you to the SimpleTableViewController view. You'll notice that you won't see a back button or any title for the views. This is because they each need their navigation items set up. Go to MainWindow.xib and click on the NavigationItem inside RootViewController and set its title to something like "Root View".
12) To add a title to the SimpleTableViewController, you'll have to drag and drop another view controller object in the MainWindow.xib that corresponds to SimpleTableViewController just like the RootViewController object corresponds to RootViewController.h. Drag a Navigation Item, and rename the title like in the last step.
13) This gets you the jist of what you need. To add additional views that can be accessed from the SimpleTableView, create them with the same process as for the RootViewController that we added. To actually add them to the table view requires a bit more understanding of how table views work and is a little bit more involved, but there are plenty of tutorials out there for that as you mentioned.
Hopefully this helps.
精彩评论