简体   繁体   English

iOS pushViewController不起作用

[英]iOS pushViewController is not working

I have a navigation controller, a view controller, and a table view that were all created in my storyboard: 我有一个在情节提要中创建的导航控制器,视图控制器和表格视图:

在此处输入图片说明

When you touch a certain row in the table view, it SHOULD push a new controller onto the navigation controller, but instead nothing happens. 当您触摸表格视图中的某一行时,它应该将新的控制器推到导航控制器上,但是什么也没有发生。 Here is the code: 这是代码:

在此处输入图片说明

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

Try 尝试

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

    UINavigationController *controller  = self.navigationController;

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

Basically instantiateViewControllerWithIdentifier creates a new instance of the specified view controller each time you call it, so you are pushing the view in a different Navigation controller than the one that is displayed 基本上, instantiateViewControllerWithIdentifier每次调用时都会创建一个指定视图控制器的新实例,因此您将视图推入与显示的导航控制器不同的导航控制器中

This worked for me. 这对我有用。 You have to define index path. 您必须定义索引路径。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM