-1
extension ReservationViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        if let vc = storyboard?.instantiateViewController(withIdentifier: "restaurantDetail") as?  RestaurantViewController {
            vc.restaurantNametxt = searchRestaurant[indexPath.row].restaurantName
            print(searchRestaurant[indexPath.row].restaurantName)
        }
        print(indexPath.row)
        print(searchRestaurant[indexPath.row].restaurantID ?? "")
        restaurantsTableView.deselectRow(at: indexPath, animated: true)
        performSegue(withIdentifier: "restaurantDetail", sender: self)
    }
}

class RestaurantViewController: UIViewController {
    
    @IBOutlet weak var restaurantName: UILabel!
    @IBOutlet weak var reserverButton: UIButton!
    
    var restaurantNametxt: String = ""
    
    override func viewDidLoad() {
        super.viewDidLoad()
        restaurantName.text = restaurantNametxt
        print(restaurantNametxt)
    }
    
    
    @IBAction func reserverButtonPressed(_ sender: UIButton) {
        performSegue(withIdentifier: "FinalStep", sender: self)
        self.navigationItem.hidesBackButton = true
    }
}

When the user performs didSelectRow at I want to pass searchRestaurant[indexPath.row].restaurantName to another vc which is RestaurantViewController but on UILabel is still "".

vc.restaurantNametxt contains the real restaurantName because it is printing the value.

Joke coder
  • 247
  • 1
  • 10

1 Answers1

1

Your approach fails because you do not use the vc you instantiated. With performSegue you create a new ViewController that gets presented. And its property is empty as you already discovered.

Try:

self.presentViewController(vc, animated: true, completion: nil)

and remove:

performSegue(withIdentifier: "restaurantDetail", sender: self)
HangarRash
  • 7,314
  • 5
  • 5
  • 32
burnsi
  • 6,194
  • 13
  • 17
  • 27
  • Yeah it works but it opens next view controller Modally as a new layer. Not like a navigation controller with back button on top. – Branislav Fekeč Jan 14 '23 at 12:37
  • @BranislavFekeč please update your question to include all requirements you have for this. It´s hard to compile an apropriate answer without the information how this is structured and what should and should not happen. – burnsi Jan 14 '23 at 14:30