didSelectRowAtIndexPath 不以编程方式导航到destinationView
Posted
技术标签:
【中文标题】didSelectRowAtIndexPath 不以编程方式导航到destinationView【英文标题】:didSelectRowAtIndexPath don't navigate to destinationView programmatically 【发布时间】:2018-09-12 08:58:37 【问题描述】:我在另一个名为 ViewController 的视图中使用 UITableView 作为 childView,在 UITableView 中,我希望单元格可以点击并导航到destinationView,而不使用storyBoard,所以我在didSelectRowAtIndexPath 上实现了这段代码
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
NSLog(@"helloWorld!");
DescriptionViewController *desc = [[DescriptionViewController alloc]
initWithNibName:@"DescriptionViewController" bundle:nil];
[self.navigationController pushViewController:desc animated:YES];
【问题讨论】:
既然你没有使用 Storyboards,你确定你已经正确设置了父 UINavigationController 吗?这将有助于显示导航控制器的代码。 检查 self.navigationController 是否为零? self.navigationController 返回 nil 试试 [self.parentViewController.navigationController pushViewController:desc animated:YES]; 【参考方案1】:你有一个 nil 导航
[self.navigationController pushViewController:desc animated:YES];
所以请确保当前的 VC 嵌入在导航控制器中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
MainViewController *main = [[MainViewController alloc ] initWithNibName:@"MainViewController" bundle:nil];
UINavigationController*nav = [[UINavigationController alloc] initWithRootViewController:main];
self.window.rootViewController = nav;
return YES;
如果你甚至不使用 xibs ,那么像这样加载 vc
DescriptionViewController*desc = [[DescriptionViewController alloc] init];
替换:
[UIApplication sharedApplication].keyWindow.rootViewController = desc;
【讨论】:
如果我把这个异常出现'NSInternalInconsistencyException',原因:'Could not load NIB in bundle:'NSBundle (loaded)' with name 'DescriptionViewController'' 如果你不使用故事板,你应该有一个 nib 名称 DescriptionViewController.xib,请参阅编辑,我想你推送的 vc 是 MainViewController,它也有一个 nib 感谢这项工作,但它保留了旧的 ViewContoller,或者当我点击它导航到另一个视图的单元格时,它保留了我想要的,而不仅仅是通过新视图更改子视图(DescriptionViewContoller)【参考方案2】:如果您的视图控制器嵌入在
UINavigationController
中,则以下代码将起作用:[self.navigationController pushViewController:desc animated:YES];
否则,您将必须以如下方式显示您的视图控制器:
[self presentViewController:desc animated:YES completion:nil];
【讨论】:
如果我评论 [self presentViewController:desc animated:YES completion:nil];这不起作用,但如果我评论 [self.navigationController pushViewController:desc animated:YES];并且只能与 presentViewController 一起工作,你能解释一下吗 @ElyMan 要推送视图控制器,您需要有一个UINavigationController
。 UINavigationController 维护视图控制器的堆栈。如果视图控制器没有嵌入在 UINavigationController 中,您将无法在其上推送另一个视图控制器,但您可以以模态方式呈现它。【参考方案3】:
检查你的navigationController
是否不为零,并实例化视图控制器对象进行推送。
1.检查导航(来自情节提要):
2。实例化视图控制器:
DescriptionViewController *desc = [self.storyboard instantiateViewControllerWithIdentifier:@"DescriptionViewController"];
[self.navigationController pushViewController:desc animated:YES];
如果你的项目中有超过 1 个故事板,你可以使用,
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
DescriptionViewController *desc = [sb instantiateViewControllerWithIdentifier:@"DescriptionViewController"];
[self.navigationController pushViewController:desc animated:YES];
3.如果您的视图不在故事板上
只需初始化并推送到DescriptionViewController,
DescriptionViewController *desc = [[DescriptionViewController alloc] init];
[self.navigationController pushViewController:desc animated:YES];
在DescriptionViewController
的viewDidLoad
中,添加要加载的白色视图。
UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.width)];
whiteView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:whiteView];
【讨论】:
应用程序试图在目标DescriptionViewController *desc = [[DescriptionViewController alloc] init];
是的,这是我用笔尖初始化的问题,或者没有笔尖文件,谢谢您的回答【参考方案4】:
如果您使用堆栈结构,即父控制器嵌入在 UINavigationController 中,请尝试以下代码 sn-p:
DescriptionViewController *desc = [[DescriptionViewController alloc]
initWithNibName:NSStringFromClass([DescriptionViewController class]) bundle:nil];
[self.navigationController pushViewController:desc animated:YES];
如果您想以模态方式呈现 DescriptionViewController 并向其添加导航控制器,以下代码 sn-p 将很有用
DescriptionViewController *desc = [[DescriptionViewController alloc]
initWithNibName:NSStringFromClass([DescriptionViewController class]) bundle:nil];
UINavigationController *navCtr = [[UINavigationController alloc] initWithRootViewController: desc];
[self presentViewController:navCtr animated:YES completion:nil];
【讨论】:
以上是关于didSelectRowAtIndexPath 不以编程方式导航到destinationView的主要内容,如果未能解决你的问题,请参考以下文章
如果从 tableView:didSelectRowAtIndexPath 调用 UIWebView 不调用 loadRequest:
TableView didSelectRowAtIndexPath 不执行
textFieldDidEndEditing vs didSelectRowAtIndexPath,后者不被调用
tableView: didSelectRowAtIndexPath: 如果不设置下一个视图控制器的背景颜色看起来很糟糕