I am trying to pass the data of which name was selected by the user in my ShipViewController
to my ProfileViewController
. I tried using closures to do so, but the title of the button in ProfileViewController
(which presents the modal popover to ShipViewController
) isn't changing to the name the user selects in ShipViewController
.
Should it not be String --> () or is the way I instantiate my view controller incorrect?
(ShipViewController)
var completionHandler:((String) -> ())?
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "shipCell", for: indexPath) as! ShipViewCell
if selectedIndex == indexPath.row {
let result = completionHandler?(shipNames[selectedIndex!])
self.dismiss(animated: true, completion: nil)
}
}
(In viewDidLoad of ProfileViewController)
let vc = storyboard?.instantiateViewController(withIdentifier: "ShipViewController") as! ShipViewController
vc.completionHandler = { (text) -> ()in
print(text)
self.shipButton.setTitle(text, for: .normal)
}