如何使用闭包而不是委托将数据从一个VC传递到另一个VC? [英] How do I pass data from one VC to another using closures in swift instead of delegates?

查看:52
本文介绍了如何使用闭包而不是委托将数据从一个VC传递到另一个VC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有两个VC,一个显示一个计数器UILabel,另一个显示一个按钮,按此按钮可以使原始VC上的UILabel增加一个。

I have two VCs currently, one that displays a counter UILabel and another that displays a button, pressing which is supposed to increment the UILabel on the original VC by one.

我仍在学习Swift,并且我知道如何使用委托来解决此问题,但想学习如何使用闭包来解决这个问题,我发现不看例子就很难做;

I'm still learning Swift, and I know how to solve this issue using delegates, but want to learn how to do so using closures, which I'm finding a little difficult to do without seeing an example; hence the bountied question.

这是我第一次使用UILabel计数器进行风险投资:

Here is my first VC with the UILabel counter:

var tappedCount: Int = 10

lazy var label: UILabel = {
    let label = UILabel()
    label.text = "\(tappedCount)"
    label.textAlignment = .center
    label.font = UIFont(name: "Copperplate", size: 90)
    label.translatesAutoresizingMaskIntoConstraints = false
    return label
}()

override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = .white
    navigationItem.title = "Navigation Controller"
    navigationController?.navigationBar.isTranslucent = false
    view.addSubview(label)
    view.addSubview(button)

    let nextButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(moveToSecond))
    navigationItem.rightBarButtonItem = nextButton

    label.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    label.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
  }

@objc func moveToSecond() {
    show(SecondViewController(), sender: self)
}

这是我的第二个VC按钮:

and here is my second VC with the button:

class SecondViewController: UIViewController {

    var callback : (() -> Void)?

    @objc func buttonPressed() {
        print("hello")
    }

    let button: UIButton = {
        let button = UIButton()
        button.translatesAutoresizingMaskIntoConstraints = false
        button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)
        button.setTitle("HELLO", for: .normal)
        button.backgroundColor = .red
        return button
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        view.addSubview(button)

        button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        button.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        button.heightAnchor.constraint(equalToConstant: 100).isActive = true
        button.widthAnchor.constraint(equalToConstant: 100).isActive = true

           }
    }


推荐答案

您必须在第一个视图控制器中分配闭包并调用在第二个

You have to assign the closure in the first view controller and call it in the second

更改 moveToSecond

@objc func moveToSecond(_ sender : UIBarButtonItem) {
    let secondViewController = SecondViewController()
    secondViewController.callback = {
       self.tappedCount += 1
       self.label.text = Sting(self.tappedCount)
    }
    show(secondViewController, sender: self)
}

buttonPress

@objc func buttonPressed(_ sender : UIButton) {
    callback?()
}

操作中,将受影响的UI元素作为发件人传递是一个好习惯。

It's good practice to pass the affected UI element as sender in the action.

这篇关于如何使用闭包而不是委托将数据从一个VC传递到另一个VC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆