简体   繁体   English

如何将数据从 UIViewsController 传递到 UIView,Swift

[英]How to pass data from UIViewsController to UIView, Swift

I'm trying to pass data from my UIViewcoontroller to a UIView.我正在尝试将数据从我的 UIViewcoontroller 传递到 UIView。 I want that the amount value passed to UIView, I need it to sending request by button in UIView.我希望将金额值传递给 UIView,我需要它通过 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

    }

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.它是一个具有闭包类型的属性 completionHandler。 The closure is optional, denoted by the ?, and the closure signature is (String) -> String.闭包是可选的,用 ? 表示,闭包签名是 (String) -> String。 This means the closure has one parameter of type String and returns one value of type String.这意味着闭包有一个 String 类型的参数并返回一个 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
}

You should consider implementing the action methods in UIViewController because that is what UIViewControllers are there for.您应该考虑在 UIViewController 中实现操作方法,因为这就是 UIViewControllers 的用途。

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.但是,如果您绝对需要在 UIView 子类中实现 action 方法,您可以创建一个协议并将其称为数据源,就像我们有 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.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM