首页 > 解决方案 > 如何使用 tableview 将页面导航到另一个视图控制器确实选择方法

问题描述

这是我从一个 viewController 导航到另一个 viewControler 的代码,但我无法导航。data 是 rest api nsobject 数据,存储来自 rest api 的类别作为名称,娱乐是一个类别。当我点击类别时转到另一个视图控制器

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..!")
    }
}

标签: swift3

解决方案


可能有以下原因

  1. 检查名称是否包含娱乐或娱乐作为字符串值?表示区分大小写(作为测试做以下和检查) data[indexPath.row].name.lowerCased() == "Entertainment".lowerCased()

  2. 如果缺少将 navigationController 嵌入到您的根控制器,则缺少 navigationController(未嵌入导航控制器)。

  3. 尝试删除dismiss(animated: true, completion: nil)


推荐阅读