UINavigationController not working as intended (iPhone)
The main view of my app don't have a UINavigationController
but when a button is tapped it goes to a view to create an event. This CreateEvent view does have a UINav开发者_开发技巧igationController
to move though the differents views needed to create the new event. The UINavigationController
is displayed but can't set a title or add any navigation buttons, looking at the code can you find out why? Thanks
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: I forgot to tell that I don't have any .xib
file.
Assuming you want the table view to appear within the navigation controller, you need to do something like this:
- (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];
}
精彩评论