【问题标题】:How to get info which specific button is tapped in UIView in another view如何在另一个视图中获取在 UIView 中点击了哪个特定按钮的信息
【发布时间】:2021-08-20 22:10:48
【问题描述】:

我想为我的应用程序添加倒计时选项。有 4 种方法可以选择倒计时时间。我制作了一个单独的 UIView,我将其命名为 BreakTimeView。我那里有 4 个按钮。当按下其中一个时,我想在主视图控制器中获取信息。我希望这个信息给我一个特定的秒数,让计时器倒计时。我知道如果我在主视图控制器中从头开始制作这个视图,我可以触发我自己的动作。但是如果按钮在单独的视图中,我不知道如何通过这个秒数(Int)。我做了类似的事情,但我认为它看起来不太好,问题是我怎样才能做得更好/更聪明?

class ViewController: UIViewController {

    var value: Int = 0
    var bV = BreakTimeView()
    override func viewDidLoad() {
         super.viewDidLoad()
          
        bV = BreakTimeView(frame: CGRect(x: 0,
                                      y: view.frame.maxY + 50,
                                      width: view.frame.size.width,
                                      height: view.frame.size.width))
        view.addSubview(bV)
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(animate))
        view.addGestureRecognizer(tap)
        
        bV.checkButtonPressed = {
            self.value = 30
            print(self.value)
        }
        bV.checkButtonPressed2 = {
            self.value = 45
            print(self.value)
        }
        bV.checkButtonPressed3 = {
            self.value = 60
            print(self.value)
        }
        bV.checkButtonPressed4 = {
            self.value = 90
            print(self.value)
        }
    }
    

    @objc func animate(){
        UIView.animate(withDuration: 1) {
            self.bV.transform = CGAffineTransform(translationX: 0, y: -self.bV.frame.size.width - 50)
        } completion: { completed in
            print("Ekran wjechał")
        }

    }
    
}
class BreakTimeView: UIView{
    private let title = UILabel()
    
    private let button1 = UIButton()
    private let button2 = UIButton()
    private let button3 = UIButton()
    private let button4 = UIButton()

    private let horizonralStack1 = UIStackView()
    private let horizontalStack2 = UIStackView()
    
    private let verticalStack = UIStackView()
    
    var checkButtonPressed : (() -> ()) = {}
    
    var checkButtonPressed2 : (() -> ()) = {}
    var checkButtonPressed3 : (() -> ()) = {}
    var checkButtonPressed4 : (() -> ()) = {}
    override init(frame: CGRect) {
        super.init(frame: frame)
        layer.cornerRadius = 20
        backgroundColor = .gray
        
        configureTitile()
        configureButtons()
        
        configureHS1()
        configureHS2()
        
        addSubview(verticalStack)
        
        verticalStack.addArrangedSubview(horizonralStack1)
        verticalStack.addArrangedSubview(horizontalStack2)
        verticalStack.axis = .vertical
        verticalStack.distribution = .fillEqually
        verticalStack.spacing = 20
        verticalStack.translatesAutoresizingMaskIntoConstraints = false
        
        verticalStack.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 20).isActive = true
        verticalStack.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -20).isActive = true
        verticalStack.topAnchor.constraint(equalTo: title.bottomAnchor, constant: 20).isActive = true
        verticalStack.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -20).isActive = true
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func configureTitile() {
        title.text = "Wybierz czas przerwy"
        title.font = UIFont.systemFont(ofSize: 25)
        title.textAlignment = .center
        title.layer.cornerRadius = 20
        title.layer.masksToBounds = true
        title.backgroundColor = .red
        title.translatesAutoresizingMaskIntoConstraints = false
        
        addSubview(title)
        title.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 40).isActive = true
        title.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -40).isActive = true
        title.topAnchor.constraint(equalTo: self.topAnchor, constant: -40).isActive = true
        title.heightAnchor.constraint(equalToConstant: 80).isActive = true
    }
    private func configureButtons(){
        button1.layer.cornerRadius = 20
        button1.backgroundColor = .red
        button1.setTitle("30s", for: .normal)
        button1.titleLabel?.font = UIFont.systemFont(ofSize: 30)
        button1.addTarget(self, action: #selector(butt1), for: .touchUpInside)
        
        button2.layer.cornerRadius = 20
        button2.backgroundColor = .red
        button2.setTitle("45s", for: .normal)
        button2.titleLabel?.font = UIFont.systemFont(ofSize: 30)
        button2.addTarget(self, action: #selector(butt2), for: .touchUpInside)
        
        button3.layer.cornerRadius = 20
        button3.backgroundColor = .red
        button3.setTitle("60s", for: .normal)
        button3.titleLabel?.font = UIFont.systemFont(ofSize: 30)
        button3.addTarget(self, action: #selector(butt3), for: .touchUpInside)
        
        button4.layer.cornerRadius = 20
        button4.backgroundColor = .red
        button4.setTitle("90s", for: .normal)
        button4.titleLabel?.font = UIFont.systemFont(ofSize: 30)
        button4.addTarget(self, action: #selector(butt4), for: .touchUpInside)
    
    }
    
    @objc private func butt1(){
        checkButtonPressed()

        
    }
    @objc private func butt2(){
        checkButtonPressed2()

        
    }
    @objc private func butt3(){
        checkButtonPressed3()

        
    }
    @objc private func butt4(){
        checkButtonPressed4()

        
    }
    
    fileprivate func configureHS1() {
        horizonralStack1.addArrangedSubview(button1)
        horizonralStack1.addArrangedSubview(button2)
        horizonralStack1.axis = .horizontal
        horizonralStack1.distribution = .fillEqually
        horizonralStack1.spacing = 20

    }
    fileprivate func configureHS2() {
        horizontalStack2.addArrangedSubview(button3)
        horizontalStack2.addArrangedSubview(button4)
        horizontalStack2.axis = .horizontal
        horizontalStack2.distribution = .fillEqually
        horizontalStack2.spacing = 20
    }

当然,我不会在应用程序中打印秒数,而是将其传递给支持计时器的单独函数,但出于演示的目的,我只有这个

【问题讨论】:

    标签: ios swift uibutton uikit


    【解决方案1】:

    BreakTimeView 内替换这些回调-

    var checkButtonPressed : (() -> ()) = {}
    var checkButtonPressed2 : (() -> ()) = {}
    var checkButtonPressed3 : (() -> ()) = {}
    var checkButtonPressed4 : (() -> ()) = {}
    

    var checkButtonPressed : ((_ numberOfSeconds: Int) -> Void) = {}
    

    现在您的按钮操作将如下所示。

    @objc private func action1() {
        checkButtonPressed(30)
    }
    @objc private func action2() {
        checkButtonPressed(45)
    }
    @objc private func action3() {
        checkButtonPressed(60)
    }
    @objc private func action4() {
        checkButtonPressed(90)
    }
    

    在呼叫现场,它看起来像这样 -

    bV.checkButtonPressed = { [weak self] (numberOfSeconds) in
        self?.value = numberOfSeconds
        print(numberOfSeconds)
    }
    

    【讨论】:

      猜你喜欢
      • 2014-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多