pushViewController在UITableView中的didSelectRowAtIndexPath上没有做任何事情

4 iphone uiviewcontroller

我有一个UITableView,我已经设置了didSelectRowAtIndexPath,每次用户点击一行时都会调用它(我已经使用NSLog验证了它).

但是,我想重新加载相同的视图并更改不同的数据.例如,我正在远程Web服务器上显示目录的内容,并且一旦用户单击该行,我想重新加载视图并显示所选目录(行)的内容.

我正在使用此代码:

FirstViewController *fvController = [[FirstViewController alloc] initWithNibName:@"MainWindow" bundle:[NSBundle mainBundle]];
 fvController.currentDirectory = currentDirectory;
 [self.navigationController pushViewController:fvController animated:YES];
 [fvController release];
 fvController = nil;
Run Code Online (Sandbox Code Playgroud)

但是,它没有做任何事情,程序一直在运行.我想我错过了IB中的连接?

Rob*_*ier 6

没有任何事情发生的原因很可能self.navigationController就是nil因为你从来没有设置过.你的直觉是好的:当"没有任何反应"时,它几乎总是意味着某些东西nil,并且存在某种东西的第一个原因nil是你没有将它连接到IB中.

您的递归设计可以很好地理解视图控制器的工作方式.看起来你走在正确的道路上.一些不请自来的建议让你更进一步.而不是这段代码:

FirstViewController *fvController = [[FirstViewController alloc] initWithNibName:@"MainWindow" bundle:[NSBundle mainBundle]];
fvController.currentDirectory = currentDirectory;
Run Code Online (Sandbox Code Playgroud)

我建议看起来像这样的东西:

FirstViewController *fvController = [[FirstViewController alloc] initWithDirectoryPath:currentDirectory];
Run Code Online (Sandbox Code Playgroud)

当然,你需要写-initWithDirectoryPath:电话-initWithNibName:bundle:.这会在管理它的视图控制器中推送NIB的名称,允许您进行-currentDirectory只读或私有,因此没有人在视图控制器的后面混淆它,并且通常使代码更容易理解和维护.