Search code examples
swiftuinavigationcontrollertableviewdidselectrowatindexpath

How to make 2ndVC tableview didSelectRowAt working only if i come from 1stVC btn tap in swift


I have two Viewcontrollers..

in 2ndVC i have tableview.. here i dont want didSelectRowAt method to work.. i need only this method to work when it navigates from 1stVC btnAction

in 1stVC textfield i need to fill with 2ndVC tableview's selected row text.. so i have placed button on textfield and in btnAction i gave navigation to 2ndVC.. now i need tableview selected row text in textfield

in 2ndVC i wrote didSelectRowAt here i dont want to select any row but in this viewcontroller if i select any row its going to 1stVC.. i want to select row only if i navigate from 1stVC btn

class 2ndVC: UIViewController {

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
    let viewController = self.storyboard?.instantiateViewController(withIdentifier: "1stVC") as! 1stVC

    viewController.empLoc = empDetail[indexPath.row]

    self.navigationController?.pushViewController(viewController, animated: true)

}
}

1stVC code: if i go from here to 2ndVC then only i need to select tableview

class 1stVC: UIViewController {
@IBAction func empBtn(_ sender: Any) {
    
       let viewController = self.storyboard?.instantiateViewController(withIdentifier: "2ndVC") as! 2ndVC
       
       self.navigationController?.pushViewController(viewController, animated: true)
      
   }
   }

how to stop tableview row selecting method in 2ndVC.. and select tableview row if come from 1stVC..

please help me with code.


Solution

  • Have a variable in 2nd viewController say isfromFirstVC and set to false by default. When you are presenting 2nd VC from 1st set that variable to true (viewcontroller.isfromFirstVC = true) . In 2nd VC make use of this variable in didSelectRowAt method.