繁体   English   中英

如何使用tableview将页面导航到另一个视图控制器中并选择了方法

[英]How to navigate a page into another view controller using tableview did select method

这是我从一个viewController导航到另一个viewControler的代码,但是我无法导航。 数据是rest api nsobject数据,它存储来自rest api的类别作为名称,娱乐是类别。 当我点击类别时转到另一个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)
            }

您应该先添加

可能有以下原因

  1. 检查名称是否包含娱乐性或娱乐性作为字符串值? 表示区分大小写(作为测试,然后进行检查)data [indexPath.row] .name.lowerCased()==“ Entertainment” .lowerCased()

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

  3. 尝试删除dismiss(animated: true, completion: 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)
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM