UIViewController, UIView and View hierarchy in ios4 SDK / iphone development
Can someone exlain the view hierarchy in iphone/ipad development?
I want to know what is the "root" element I need to display a textBox on the screen.
I also would like to know how I can populate a UITableViewController subclass without having to use a NIB.
Is the minimum components is:
Window
UiViewController
UIVi开发者_如何学Pythonew
UITextBox
or
Window
UIView
UITextBox
@ Richard J. Ross III
Where do I put myTableView??
Window
myTableView
What type is myTableView?
The minimum components is that, but it is recommended to use the UIViewController
, as it allows you to do some cool stuff with UITableViews
, UITabBars
, etc. UITableViewController subclass without nib:
#import <UIKit/UIKit.h>
@interface MyController : UITableViewController
// custom methods here
@end
@implementation MyController
–(UITableViewCell *) tableView:(UITableView *) view cellForRowAtIndexPath:(NSIndexPath *) indexPath
{
return [[[UITableViewCell alloc] init] autorelease];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// initializes the table with 10 rows
return 10;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"the user selected row: %@", indexPath);
}
@end
Then, to use that custom controller:
MyController *control = [[MyController alloc] initWithStyle:UITableViewStylePlain];
// now hook it up!
myTableView.dataSource = control;
myTableView.delegate = control;
[control release];
Sorry for my syntax errors, I wrote this without an IDE (e.g. straight on stackoverflow).
The first guess is correct. A UIViewController manages a view property, which is a UIView. The controller is in charge of taking care of things when certain events occur. If the user rotates his device, the viewController is the object that hears about it. If you have coded for this event, the viewController will pass an instruction to the UIView to rotate. There is special code in the viewController that can decide what to do when the view appears, disappears, loads, unloads, etc.
UITableViewController manages a tableView. In the default XCode setup, the Table View DataSource and Delegate is the UITableViewController subclass. The delegate manages what happens when a user interacts with the table, by selecting rows, scrolling, etc. The data source houses information that populates the rows in the table, headers and footers.
If you want to show a textbox in a tableViewCell, you'll need a UITableView object, and a UITextField. You have to make the dataSource give the table a UITextField as a row, and add it to the UITableViewCell's view, which is called contentView.
精彩评论