[英]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. 这是我从一个viewController导航到另一个viewControler的代码,但是我无法导航。 data is rest api nsobject data that stored category from rest api as name, entertainment is a category . 数据是rest api nsobject数据,它存储来自rest api的类别作为名称,娱乐是类别。 when i tap on category goto another viewController 当我点击类别时转到另一个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 可能有以下原因
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() 表示区分大小写(作为测试,然后进行检查)data [indexPath.row] .name.lowerCased()==“ Entertainment” .lowerCased()
Missing navigationController (navigation controller not embedded) if missing embed navigationController to your root controller. 如果缺少,则缺少navigationController(未嵌入导航控制器),将embeding Controller嵌入到根控制器中。
try removing dismiss(animated: true, completion: nil)
尝试删除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)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.