Swift3 如何使用tableview did select方法将页面导航到另一个视图控制器

Swift3 如何使用tableview did select方法将页面导航到另一个视图控制器,swift3,Swift3,这是我从一个viewController导航到另一个viewController的代码,但我无法导航。数据是以rest api中的类别作为名称存储的rest api nsobject数据,娱乐是一个类别。当我点击类别转到另一个viewController时 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { print("hello welcome on this news pag

这是我从一个viewController导航到另一个viewController的代码,但我无法导航。数据是以rest api中的类别作为名称存储的rest api nsobject数据,娱乐是一个类别。当我点击类别转到另一个viewController时

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
    print("hello welcome on this news page")

    // dismiss(animated: true, completion: nil)

    if data[indexPath.row].name == "Entertainment"
    {
         let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
         let destination = storyboard.instantiateViewController(withIdentifier: "WhiteViewController") as! FourthViewController
         navigationController?.pushViewController(destination, animated: true)
         dismiss(animated: true, completion: nil)
         print("Welcome on Business..!")
    }
}

如果您正在使用故事板,请使用以下命令-

if let navigationObject = UIStoryboard(name: "storyboard_name", bundle: nil).instantiateViewController(withIdentifier: "viewcontroller_storyboardId") as? viewController_name {

   navigationController?.pushViewController(navigationObject, animated: true)
            }

可能有以下原因

  • 检查名称是否包含娱乐或娱乐作为字符串 价值表示区分大小写(作为测试,请遵循和检查) 数据[indexath.row].name.lowerCased()==“娱乐”。lowerCased()

  • 如果缺少嵌入,则缺少navigationController(导航控制器未嵌入) 导航控制器到根控制器

  • 尝试删除
    disclose(动画:true,完成:nil)

  • 用这个,

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {
        if data[indexPath.row].name == "Entertainment" {
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let whiteVC = storyboard.instantiateViewController(withIdentifier: "WhiteViewController") as! WhiteViewController
            self.navigationController?.pushViewController(whiteVC, animated: true)
        }
    }
    

    删除这行代码解散(动画:true,完成:nil)我需要在单击行@iAction func CloseSlideMenu(uSender:UIButton){解散(动画:true,完成:nil)}时关闭did select方法上的幻灯片菜单。您的故事板中是否嵌入了导航控制器?谢谢您的建议。。!但我必须使用故事板ID。这是我的故事板ID的图像