Search code examples
swiftuitableviewuiviewseguepopupwindow

Send Data from UIViewController to UIView


Iam trying to send data from a UIViewController to a UIView. I have looked everywhere but none seem to work correctly. As shown below, I am trying to send popup.priceLabel.text and popup.notificationLabel.text from a UIViewcontroller to UIView when the user selects a particular row. I tried to use segue's, protocols and other methods but nothing seems to work. Below is what I am trying to accomplish(I know its incorrect but just to illustrate intent):

//UIViewController

class CouponsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("selected = \(indexPath.row)")
    let popup = PopUpWindow()
    popup.priceLabel.text = "100.00"
    popup.notificationLabel.text = "Store"
    handleShowPopUp()
}

@objc func handleShowPopUp() {
    popUpWindow.showSuccessMessage = success
    success = !success
    popUpWindow.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
    popUpWindow.alpha = 0
}
}
//UIView

class PopUpWindow: UIView {
    var delegate: PopUpDelegate?

    var showSuccessMessage: Bool? {
        didSet {
            guard showSuccessMessage != nil else { return }
            priceLabel.text = ""
            notificationLabel.text = ""
            priceLabel.textColor = UIColor(red: 147/255, green: 227/255, blue: 105/255, alpha: 1)
        }
    }
}

Solution

  • I think you tried your segue inside didSelectRowAt() method but, it will not work because you are trying to pass data without initializing priceLabel notificationLabel so there is another way to accomplish it. You need create a variable in your popupView for your price and notification.

    var price: String = ""
    

    Inside your didSelectRowAt() method what you need to do is popup.price = "100.0". Then inside the popup's viewDidLoad() method you need to do following.

    priceLabel.text = price