尝试构build一个顶部导航栏的桌面视图

这是我使用的代码。 我错过了什么?

- (void)loadView { CGSize screen_size = [[UIScreen mainScreen] bounds].size; CGFloat navBarHeight = 40; UINavigationBar *nav = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, screen_size.width, navBarHeight)]; UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, navBarHeight, screen_size.width, screen_size.height - navBarHeight) style:UITableViewStylePlain]; table.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth; table.delegate = self; table.dataSource = self; table.editing = YES; [table reloadData]; [self.view addSubview:nav]; [self.view addSubview:table]; [nav release]; [table release]; } 

而不是在下面的一个表导航栏,我在状态栏下面的黑屏。

您需要在您的loadView方法中创build一个包含视图,并将其设置为视图控制器上的视图:

 - (void)loadView { CGSize screen_size = [[UIScreen mainScreen] bounds].size; UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0,0,screen_size.width,screen_size.height)]; self.view = myView; CGFloat navBarHeight = 40; UINavigationBar *nav = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, screen_size.width, navBarHeight)]; UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, navBarHeight, screen_size.width, screen_size.height - navBarHeight) style:UITableViewStylePlain]; table.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth; table.delegate = self; table.dataSource = self; table.editing = YES; [table reloadData]; [self.view addSubview:nav]; [self.view addSubview:table]; [nav release]; [table release]; [myView release]; } 

或者,如果你有一个与你的视图控制器相关联的nib文件,那么你应该使用viewDidLoad方法,而不是loadView。