iOS pushViewController无法正常工作

时间:2015-01-07 10:11:45

标签: ios uinavigationcontroller storyboard pushviewcontroller

我有一个导航控制器,一个视图控制器和一个表格视图,这些都是在我的故事板中创建的:

enter image description here

当您触摸表格视图中的某一行时,它应该将新控制器推送到导航控制器上,但没有任何反应。这是代码:

enter image description here

- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath {
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];

UINavigationController *controller = (UINavigationController*)[mainStoryboard
                                                               instantiateViewControllerWithIdentifier: @"dbNav"];

SubLevelViewController *slController = [[SubLevelViewController alloc] initWithStyle:UITableViewStylePlain];
[controller pushViewController: slController animated: YES];
}

2 个答案:

答案 0 :(得分:2)

尝试

- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath {

    UINavigationController *controller  = self.navigationController;

    SubLevelViewController *slController = [[SubLevelViewController alloc] initWithStyle:UITableViewStylePlain];
    [controller pushViewController: slController animated: YES];
 }

基本上instantiateViewControllerWithIdentifier每次调用时都会创建指定视图控制器的新实例,因此您要在与显示的控制器不同的导航控制器中推送视图

答案 1 :(得分:0)

这对我有用。您必须定义索引路径。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ServicesModel *service = [services objectAtIndex:indexPath.row];
    ServiceViewController *serviceViewController = [self.storyboard        instantiateViewControllerWithIdentifier:@"ServiceView"];
    serviceViewController.serviceModel = service;
    [self.serviceController pushViewController:serviceViewController animated:YES];
}