Ddmkdmfcm
Ddmkdmfcm

Reputation: 41

How to pass data from UIViewsController to UIView, Swift

I'm trying to pass data from my UIViewcoontroller to a UIView. I want that the amount value passed to UIView, I need it to sending request by button in UIView.

//this is my UIViewController

final class PayParkingViewController: BaseViewController {
    @IBOutlet weak var amount: UITextField!
//for example I need this myAmount value to be passed in my UIView
myAmount = amount.text
}
//This is my UIView
final class PaymentChoice: UIView {

    @IBOutlet weak var mastercard: UIButton!
    @IBOutlet var contentView: UIView!


      override init(frame: CGRect) {
         super.init(frame: frame)

         commonInit()
     }

     required init?(coder aDecoder: NSCoder) {
         super.init(coder: aDecoder)
         commonInit()
     }

     private func commonInit() {
             Bundle.main.loadNibNamed(className, owner: self, options: nil)
         guard let content = contentView else { return }
         addSubview(content)
         content.frame = bounds
         content.autoresizingMask = [.flexibleWidth, .flexibleHeight]
     }
    @IBAction func mastercardIsSelected(_ sender: Any) {

// here I need myAmount value from my UIViewController

    }

Upvotes: 1

Views: 789

Answers (2)

nishith Singh
nishith Singh

Reputation: 2998

You should consider implementing the action methods in UIViewController because that is what UIViewControllers are there for.

But if you absolutely need to implement the action method in UIView subclass you can create a protocol and call it data source like we have UITableViewDatasource.

Please consider the following example for clarification.

final class PaymentChoice: UIView {

//All your previous code
    weak var dataSource: PaymentChoiceDataSource?
    @IBAction func mastercardIsSelected(_ sender: Any) {
        print(dataSource?.amount)
    }
}


protocol PaymentChoiceDataSource: class {
    var amount: String { get }
}

final class PayParkingViewController: UIViewController, PaymentChoiceDataSource {
    var amountTextField: UITextField!
    var amount: String { return amountTextField.text! }
    var paymentChoiceView: PaymentChoice?

    func setupPaymentChoiceView() {
        amountTextField = UITextField(frame: CGRect.zero)
        amountTextField.text = "100"
        paymentChoiceView = PaymentChoice()
        paymentChoiceView?.dataSource = self
        paymentChoiceView?.mastercardIsSelected(amountTextField)
    }
}

let payParkingViewController = PayParkingViewController()
payParkingViewController.setupPaymentChoiceView()

Hope this helps.

Upvotes: 0

Jayesh Patel
Jayesh Patel

Reputation: 1104

You start with creating a property on the view controller, like this:

var completionHandler:((String) -> String)?

It’s a property completionHandler that has a closure type. The closure is optional, denoted by the ?, and the closure signature is (String) -> String. This means the closure has one parameter of type String and returns one value of type String.

Once more, in the view controller, call the closure when a text is texted:

let myTextedAmount = completionHandler?(amount.text)

Then, in the view you can define the closure like this:

vc.completionHandler = { text in

    print("text = \(text)")

    return text
}

Upvotes: 1

Related Questions