2010-11-13 44 views
0

我的应用程序的主视图没有UINavigationController,但是当轻触一个按钮时,它会转到视图以创建事件。这个CreateEvent视图确实有一个UINavigationController来移动创建新事件所需的不同视图。将显示UINavigationController,但无法设置标题或添加任何导航按钮,查看代码可以找出原因吗?由于UINavigationController无法按预期工作(iPhone)

CreateNewEventViewController.h

@interface CreateNewEventViewController : UIViewController { 

    UINavigationController *navigationController; 
    NewEventTableViewController *tableViewController; 
} 

CreateNewEventViewController.m

- (void)viewDidLoad { 
    [super viewDidLoad]; 


    tableViewController = [[NewEventTableViewController alloc] init]; 
    tableViewController.view.center = CGPointMake(tableViewController.view.frame.size.width/2, 
                tableViewController.view.frame.size.height/2 + 44); 
    [self.view addSubview:tableViewController.view]; 

    navigationController = [[UINavigationController alloc] init]; 
    //without this instruction, the tableView appears blocked 
    navigationController.view.frame = CGRectMake(0, 0, 320, 44); 
    navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque; 
    //title appears empty and buttons don't appear 
    self.navigationItem.title = @"Create event"; 

    [self.view addSubview:navigationController.view]; 

} 

NewEventTableViewController.h

@interface NewEventTableViewController : UITableViewController { 

} 

PS:我忘了告诉我没有任何.xib文件。

回答

2

假设你想要的表视图导航控制器内出现,你需要做的是这样的:

- (void)viewDidLoad { 
    [super viewDidLoad]; 


    tableViewController = [[NewEventTableViewController alloc] init]; 
    tableViewController.view.center = CGPointMake(tableViewController.view.frame.size.width/2, 
                tableViewController.view.frame.size.height/2 + 44); 

// remove this: [self.view addSubview:tableViewController.view]; 

    navigationController = [[UINavigationController alloc] initWithRootViewController:tableViewController]; // <-- do this instead 
    //without this instruction, the tableView appears blocked 
    navigationController.view.frame = CGRectMake(0, 0, 320, 460); // <-- nav controller should fill the screen 
    navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque; 

    //title appears empty and buttons don't appear 
    tableViewController.navigationItem.title = @"Create event"; // <-- something like this 

    [self.view addSubview:navigationController.view]; 

} 
+0

谢谢!它终于工作了,我真的应该深入了解UINavigationController主题。只是评论说,从tableViewController.navigationItem.title = @“创建事件”设置标题;没有工作,但它从NewEventTableViewController.m中的viewDidLoad中完成 – framara 2010-11-13 12:57:16