【问题标题】:didSelectRowAtIndexPath don't navigate to destinationView programmaticallydidSelectRowAtIndexPath 不以编程方式导航到destinationView
【发布时间】: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];

标签: ios objective-c uitableview


【解决方案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];

DescriptionViewControllerviewDidLoad 中,添加要加载的白色视图。

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

【讨论】:

  • 应用程序试图在目标 上推送一个 nil 视图控制器。
  • 您需要在情节提要“DescriptionViewController”中为您的视图控制器提供标识符。
  • thnx 但我说我没有使用 storyBoard 我正在以编程方式编写所有内容
  • 因为你可以简单地使用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];

【讨论】:

    猜你喜欢
    • 2018-02-15
    • 2013-01-17
    • 1970-01-01
    • 1970-01-01
    • 2023-02-20
    • 2014-01-18
    • 2017-10-27
    • 2015-03-09
    • 1970-01-01
    相关资源
    最近更新 更多