0

我的应用程序的主视图没有,UINavigationController但是当点击按钮时,它会转到视图以创建事件。这个 CreateEvent 视图确实可以UINavigationController在创建新事件所需的不同视图之间移动。显示UINavigationController但不能设置标题或添加任何导航按钮,查看代码你能找出原因吗?谢谢

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文件。

4

1 回答 1

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];

}
于 2010-11-13T12:40:42.457 回答