通过一个功能以编程方式迅速触发多个按钮

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过一个功能以编程方式迅速触发多个按钮相关的知识,希望对你有一定的参考价值。

试图总结我的代码。问题是我收到“ newCalculator [2861:53607]-[UIButton buttonClicked:]:无法识别的选择器发送到实例0x7fd912651e50”错误,并试图了解我需要做什么?我研究了一些更改选择器语法的解决方案,但是此问题仍然存在。我想要的是在stackview内,我添加了一个带有顺序的按钮,并且所有按钮在单击时都应调用相同的功能。

   o//
//  ViewController.swift
//  newCalculator
//
//  Created by taylank on 17.10.2019.
//  Copyright © 2019 TKT. All rights reserved.
//

import UIKit
var stackViews = [UIStackView]()
class ViewController: UIViewController {


    override func viewDidLoad() {
        super.viewDidLoad()

        //Layer 1
        let containerStackView1 = UIStackView()
        containerStackView1.translatesAutoresizingMaskIntoConstraints = false
        containerStackView1.axis = .vertical
        containerStackView1.distribution = .fillEqually
        containerStackView1.spacing = 1

        //Layer 2
        let topHalfView2 = UIView()
        topHalfView2.translatesAutoresizingMaskIntoConstraints = false
        topHalfView2.backgroundColor = .white
        let botHalfView2 = UIView()
        botHalfView2.translatesAutoresizingMaskIntoConstraints = false


        //Layer 3
        let topLabel3 = UILabel()
        topLabel3.translatesAutoresizingMaskIntoConstraints = false
        topLabel3.text = "0"
        topLabel3.font = UIFont.boldSystemFont(ofSize: 40)

        let containerStackView3 = UIStackView()
        containerStackView3.translatesAutoresizingMaskIntoConstraints = false
        containerStackView3.axis = .vertical
        containerStackView3.distribution = .fillEqually
        containerStackView3.spacing = 1

        //Layer 4
        stackViews = createStackViews(from: 0, to: 4, align: "h")
        let yamaView = UIView()
        yamaView.translatesAutoresizingMaskIntoConstraints = false
        yamaView.backgroundColor = .black

        //ADDING VIEWS TO SCENE .VIEW->Contstckview1->Top-BotHalfView->contstckview2-ResultLabel->horzstackviews
        containerStackView1.addArrangedSubview(topHalfView2)
        containerStackView1.addArrangedSubview(botHalfView2)
        view.addSubview(containerStackView1)
        topHalfView2.addSubview(topLabel3)
        botHalfView2.addSubview(containerStackView3)

        containerStackView3.addArrangedSubview(stackViews[0])
        view.addSubview(yamaView)

        //stackViews[0].topAnchor.constraint(equalTo: containerStackView3.topAnchor, constant: 0).isActive = true

        for a in 1...stackViews.count-1{
              containerStackView3.addArrangedSubview(stackViews[a])

           // stackViews[a].leadingAnchor.constraint(equalTo: stackViews[0].leadingAnchor, constant: 0).isActive = true
            // stackViews[a].trailingAnchor.constraint(equalTo: stackViews[0].trailingAnchor, constant: 0).isActive = true



            }
        //stackViews[0].leadingAnchor.constraint(equalTo: containerStackView3.leadingAnchor, constant: 0).isActive = true
      //  stackViews[0].trailingAnchor.constraint(equalTo: containerStackView3.trailingAnchor, constant: 0).isActive = true

        //Uiheight--v:?--uibottom--uifillequ

        //ConstraintsStackView1
        containerStackView1.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
        containerStackView1.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
        containerStackView1.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 0).isActive = true
        containerStackView1.heightAnchor.constraint(equalToConstant: view.frame.height).isActive = true

        //TopHalfView2
        topHalfView2.topAnchor.constraint(equalTo: containerStackView1.topAnchor, constant: 0).isActive = true
        topHalfView2.leadingAnchor.constraint(equalTo: containerStackView1.leadingAnchor, constant: 0).isActive = true
        topHalfView2.trailingAnchor.constraint(equalTo: containerStackView1.trailingAnchor, constant: 0).isActive = true
       // topHalfView2.heightAnchor.constraint(equalToConstant: containerStackView1.frame.height/2).isActive = true
        //creates problem conflicts with other constraint fill equally?


        //BotHalfView2
        botHalfView2.bottomAnchor.constraint(equalTo: containerStackView1.bottomAnchor, constant: 0).isActive = true
        botHalfView2.leadingAnchor.constraint(equalTo: containerStackView1.leadingAnchor, constant: 0).isActive = true
        botHalfView2.trailingAnchor.constraint(equalTo: containerStackView1.trailingAnchor, constant: 0).isActive = true
       // botHalfView2.heightAnchor.constraint(equalToConstant: containerStackView1.frame.height/2).isActive = true

         //topLabel3
        topLabel3.bottomAnchor.constraint(equalTo: topHalfView2.bottomAnchor, constant: 0).isActive = true
        topLabel3.trailingAnchor.constraint(equalTo: topHalfView2.trailingAnchor, constant: -5).isActive = true

        //ConstraintsStackView3
            containerStackView3.leadingAnchor.constraint(equalTo: botHalfView2.leadingAnchor, constant: 0).isActive = true
            containerStackView3.trailingAnchor.constraint(equalTo: botHalfView2.trailingAnchor, constant: 0).isActive = true
            containerStackView3.topAnchor.constraint(equalTo: botHalfView2.topAnchor, constant: 0).isActive = true
            containerStackView3.bottomAnchor.constraint(equalTo: botHalfView2.bottomAnchor, constant: 0).isActive = true


        //constraint button

       stackViews[4].arrangedSubviews[2].widthAnchor.constraint(equalTo: stackViews[4].arrangedSubviews[1].widthAnchor,multiplier: 1).isActive = true //sonbutton bir yandakine
        stackViews[4].arrangedSubviews[1].widthAnchor.constraint(equalTo: stackViews[4].arrangedSubviews[0].widthAnchor,multiplier: 0.5).isActive = true // ortadaki buton en soldakine

        yamaView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        yamaView.topAnchor.constraint(equalTo: stackViews[4].bottomAnchor).isActive = true
        yamaView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
       yamaView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true


    }


}
func createButtons(_ named: String...)->[UIButton]{
    var c = 0
return named.map { name in

        let button = UIButton()
        button.translatesAutoresizingMaskIntoConstraints = false
        button.setTitle(name, for: .normal)
        button.backgroundColor = UIColor.gray
        button.setTitleColor(.white, for: .normal)
        button.layer.cornerRadius = 1
       button.addTarget(button, action: #selector(buttonClicked) ,for:.touchUpInside)


        return button


}



}
@objc func buttonClicked(_ sender:UIButton){

      print("clicked")
  }

func createStackViews(from x: Int,to y:Int, align type: String)->[UIStackView]{
    // use tag instead of name---
    //use dictionary..keys name values



    if (type == "v"){


        for a in x...y{

            let stackVertical = UIStackView()
                stackVertical.translatesAutoresizingMaskIntoConstraints = false
                stackVertical.axis = .vertical
                stackVertical.spacing = 1
                stackVertical.distribution = .fillEqually
                stackVertical.tag = a

        }





    }
    else if(type=="h"){
        var counter = 0
        for a in x...y{
                    counter += 1

                    switch counter{
                    case 1: let stackHorizontal = UIStackView(arrangedSubviews:createButtons("Clear","+/-","%","÷"))

                    stackHorizontal.translatesAutoresizingMaskIntoConstraints = false
                    stackHorizontal.axis = .horizontal
                    stackHorizontal.spacing = 1
                    stackHorizontal.distribution = .fillEqually
                    stackHorizontal.tag = 90+a
                    stackViews.append(stackHorizontal)

                    case 2: let stackHorizontal = UIStackView(arrangedSubviews:createButtons("7","8","9","x"))

                    stackHorizontal.translatesAutoresizingMaskIntoConstraints = false
                    stackHorizontal.axis = .horizontal
                    stackHorizontal.spacing = 1
                    stackHorizontal.distribution = .fillEqually
                    stackHorizontal.tag = 100 + a
                    stackViews.append(stackHorizontal)

                    case 3: let stackHorizontal = UIStackView(arrangedSubviews:createButtons("4","5","6","-"))

                    stackHorizontal.translatesAutoresizingMaskIntoConstraints = false
                    stackHorizontal.axis = .horizontal
                    stackHorizontal.spacing = 1
                    stackHorizontal.distribution = .fillEqually
                    stackHorizontal.tag = 110+a
                    stackViews.append(stackHorizontal)

                    case 4: let stackHorizontal = UIStackView(arrangedSubviews:createButtons("1","2","3","+"))

                     stackHorizontal.translatesAutoresizingMaskIntoConstraints = false
                     stackHorizontal.axis = .horizontal
                     stackHorizontal.spacing = 1
                     stackHorizontal.distribution = .fillEqually
                     stackHorizontal.tag = 120 + a
                     stackViews.append(stackHorizontal)

                    case 5: let stackHorizontal = UIStackView(arrangedSubviews:createButtons("0",",","="))

                     stackHorizontal.translatesAutoresizingMaskIntoConstraints = false
                     stackHorizontal.axis = .horizontal
                     stackHorizontal.spacing = 1
                    stackHorizontal.distribution = .fillProportionally
                     stackHorizontal.tag = 130 + a
                     stackViews.append(stackHorizontal)
                    default:
                        break
            }

             }


        return stackViews
    }



    return [UIStackView.init()]
 }
答案

替换

button.addTarget(button, action: "buttonClicked:" ,for:.touchUpInside)

with

button.addTarget(self, action: #selector(buttonClicked) ,for:.touchUpInside)

@objc func buttonClicked(_ sender:UIButton){

以上是关于通过一个功能以编程方式迅速触发多个按钮的主要内容,如果未能解决你的问题,请参考以下文章

以编程方式切换片段

Android 以编程方式打开手电筒按钮

以编程方式触发控件的事件

根据活动(即可见)片段以编程方式启用或禁用按钮

Android:以编程方式在片段中添加多个 RecyclerView

如何在android中以编程方式在片段之间导航?