简体   繁体   中英

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

This is my code to navigate from one viewController to another viewControler but i can't navigate. data is rest api nsobject data that stored category from rest api as name, entertainment is a category . when i tap on category goto another 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 you are using storyboard use this -

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

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

您应该先添加

There could be come following reasons

  1. Check if name contains entertainment Or Entertainment as string value? means case sensitivity(as a test do following and check) data[indexPath.row].name.lowerCased() == "Entertainment".lowerCased()

  2. Missing navigationController (navigation controller not embedded) if missing embed navigationController to your root controller.

  3. try removing dismiss(animated: true, completion: nil)

Simply Use This,

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)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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