ios - 应用程序试图在 didSelectRowAtIndexPath 中将一个 nil View Controller 推送到目标上

标签 ios objective-c uitableview storyboard didselectrowatindexpath

我们有两个 UIViewControllers

  • firstViewController

  • 第二 View Controller

。当我们点击 UItableViewCell 时,我们希望它转到 SecondViewController

我们这样试过

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

   destinationController = [[UserDetails alloc] init];
   destinationController = (UserDetails *)[storyboard instantiateViewControllerWithIdentifier:@"User"];
   destinationController.FriendlyName.text=[usernameFriendlyName objectAtIndex:indexPath.row];
   UIBarButtonItem * backButton =[[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:nil];
   destinationController.navigationItem.backBarButtonItem = backButton;
  [self.navigationController pushViewController:destinationController animated:TRUE];
}

请指导我们。我们的代码有什么问题。

最佳答案

不需要做这一切。只需将两个 viewController 与 Push segue 连接起来。 给 segue 一些标识符 并简单地将这一行写在 didSelectRowAtIndexPath

[self performSegueWithIdentifier: @"Your segue identifier" sender: self];

要将数据从一个 View 传递到另一个 View ,请使用 PrepareForSegue 方法

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"Your segue identifier"])
    {
        // Get reference to the destination view controller
        YourViewController *vc = [segue destinationViewController];

        // Pass any objects to the view controller here, like...
        [vc setMyObjectHere:object];
    }
}

关于ios - 应用程序试图在 didSelectRowAtIndexPath 中将一个 nil View Controller 推送到目标上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32505160/

相关文章:

ios - 如何在 OSX 3.2.1 上的 xCode 服务上添加远程存储库?

iphone - 如何更改焦点单元格上的文本颜色?

swift - 字典无法正常工作

ios - 为 UITableView 的索引设置动画的最佳方式?

ios - crt1.3.1.o到底是什么

ios - 拉动刷新不适用于 Alamofire 和 Swift 3

objective-c - 编写远程 I/O 渲染回调函数时遇到问题

iphone - 将不同的图像设置到 uitableview 单元格

ios - 突出显示 UITableView 而不选择

ios - 任何更改时 Xcode 8 自动构建