试图创建在 swift 上按下时访问按钮的方法?

问题描述

import UIKit
import SpriteKit
import GameplayKit

struct Question {   
    var Question : String!
    var Answers : [String]!
    var AnswerNumber : Int!
}

class LevelOne: SKScene {
    
    var button = Button()
    
    var buttons = [Button]()
    let Question_met = UILabel(frame: CGRect(x: 0.3,y: 0.3,width: 40,height: 21))
    
    var Questions = [Question]()
       
    var QNumber = Int()
    
    var buttonNames = [""]
    
    override func didMove(to view: SKView) {
        
        let background = SKSpriteNode(imageNamed: "background")
        background.position = CGPoint(x: self.size.width/2,y: self.size.height/2)
        background.zPosition = 0
        self.addChild(background)
        
        QuestionFunction()
        
        ButtonFuction()
         
        PickQuestion()
        
    }
    
    @objc func QuestionFunction(){
        
        Questions = [Question(Question: "What is a Car ?",Answers: ["Bat","Cat","Vehicle","Pat"],AnswerNumber: 3),Question(Question: "What is a plane ?","Aircraft",AnswerNumber: 2),Question(Question: "What is a ant ?","vegetable","Insect"],AnswerNumber: 4),Question(Question: "What is a Apple ?","Fruit",]
        
        Question_met.frame = CGRect(x: 330,y: 70,width: 200,height: 21)
        Question_met.backgroundColor = UIColor.orange
        Question_met.textColor = UIColor.white
        Question_met.textAlignment = NSTextAlignment.center
        Question_met.text = "What caused Global Warming ?"
        self.view?.addSubview(Question_met)
        
    }
    
    @objc func ButtonFuction(){
        
        let stacView = UIStackView()
        stacView.spacing = 12
        stacView.distribution = .fillEqually
        stacView.axis = .horizontal
        stacView.translatesAutoresizingMaskIntoConstraints = false
        view!.addSubview(stacView)
        
        buttonNames = ["One","Two","Three","Four"]
        
        for name in buttonNames {
            button = Button()
            button.setTitle(name,for: .normal)
            stacView.addArrangedSubview(button)
            buttons.append(button)
        }
    
        NSLayoutConstraint.activate([stacView.centerXAnchor.constraint(equalTo: view!.centerXAnchor),stacView.centerYAnchor.constraint(equalTo: view!.centerYAnchor),stacView.widthAnchor.constraint(equalToConstant: 350),stacView.heightAnchor.constraint(equalToConstant:70)])
        
    }
    
    @objc func PickQuestion(){
        
        if Questions.count > 0{
            QNumber = 0
            Question_met.text = Questions[QNumber].Question
            
            for i in 0..<buttons.count{
                buttons[i].setTitle(Questions[QNumber].Answers[i],for: .normal)
            }
            Questions.remove(at: QNumber)
        }
        else {
            print("Done!")
        }
    }
    
    @objc func handleButton() {
        
        print("")
        
    }
    
    override func touchesBegan(_ touches: Set<UITouch>,with event: UIEvent?) {
    

    }
}

我创建了一个基于测验的游戏并手动编写了我的按钮,但不确定如何创建在按下按钮时处理的方法

解决方法

不完全确定你在问什么,所以要说明我认为你在问什么。

您只是询问如何将按钮与操作联系起来。

您的按钮在循环中声明为 button = UIButton(),那么操作将在下面声明为 handleButton 方法。

您可以简单地为按钮执行 addTarget(:)

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

您可以谷歌Swift Classname,即Swift UIButton来确定该类的方法。例如在 UIButton 中,我们可以看到有一个标识为 addTarget(_:action:for) 的方法。

来自 Apple 的文档:

您可以使用 addTarget(_:action:for:) 方法或通过在 Interface Builder 中创建连接将按钮连接到您的操作方法。操作方法的签名采用三种形式之一,清单 1 中列出了这些形式。选择提供响应按钮点击所需信息的形式。

编辑: OP 决定询问如何在循环中分配按钮功能。为此,您需要一个可以引用的函数列表,也就是 Selector

let selectors:[Selector] = [#selector(handleButton1),#selector(handleButton2,#selector(handleButton3)]

for i in 0..<3 {
    button.addTarget(self,action: selectors[i],for: .touchUpInside)
}