简体   繁体   中英

XCode swift 3 if current viewController navigate in tableView didSelect

I'm trying to navigate to a specific viewController. i have the same tableView outlet in both viewControllers one is main VC and second is requestHistory VC. for example if the user was on the first viewController and select cell in tableview it should navigate to a specific viewController and the same thing for the second VC if the user was on the second VC and selected the same cell it should redirect to another VC. please note that i'm using the same table view cell outlet for both VC and the desired result is each VC storyboard should redirect to their specific destination.i'm not sure about the error i'm facing below here is my code so far:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("Row \(indexPath.row) selected")

    var currentStoryboard = UIStoryboard(name: "DoctorHomeViewController", bundle: nil)

    if currentStoryboard == "DoctorHomeViewController" {
    let storyboardd = UIStoryboard(name: "DoctorHome", bundle: Bundle.main)
    let eventViewController = storyboardd.instantiateViewController(withIdentifier: "EventDetailsNav") as? UIViewController
    self.navigationController?.pushViewController(eventViewController!, animated: true)
    }

    else{
        let storyboardd = UIStoryboard(name: "DoctorHome", bundle: Bundle.main)
        let eventViewController = storyboardd.instantiateViewController(withIdentifier: "RequestsHistory") as? UIViewController
        self.navigationController?.pushViewController(eventViewController!, animated: true)
    }
    }
}

Any help would be much appreciable.

first you need to mark "Use Storyboard ID" in identity inspector for respective view controller in your storyboard and then get storyboard id and compare it with yourView controller.

 let currentStoryboard : String! = self.restorationIdentifier
    if currentStoryboard == "DoctorHomeViewController" {
        let storyboard = UIStoryboard(name: "DoctorHome", bundle: Bundle.main)
        let eventViewController = storyboard.instantiateViewController(withIdentifier: "EventDetailsNav") as! EventDetailsNav
        self.navigationController?.pushViewController(eventViewController!, animated: true)
    }

    else{
        let storyboard = UIStoryboard(name: "DoctorHome", bundle: Bundle.main)
        let eventViewController = storyboard.instantiateViewController(withIdentifier: "RequestsHistory") as RequestsHistory
        self.navigationController?.pushViewController(eventViewController!, animated: true)
    }

hope this will help you.

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