【问题标题】:Trying to create method to access the buttons when pressed on swift?试图创建在快速按下按钮时访问按钮的方法?
【发布时间】:2021-05-13 13:34:12
【问题描述】:
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 ?", Answers: ["Bat","Aircraft","Cat","Pat"], AnswerNumber: 2),
        
        Question(Question: "What is a ant ?", Answers: ["Bat","Cat","Vegetable","Insect"], AnswerNumber: 4),
        
        Question(Question: "What is a Apple ?", Answers: ["Bat","Cat","Fruit","Pat"], AnswerNumber: 3),]
        
        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()?为什么要使用 UIKit 来制作按钮?

标签: swift xcode button sprite-kit skspritenode


【解决方案1】:

不完全确定您的要求,因此请说明我认为您的要求。

您只是在询问如何将按钮连接到操作。

您的按钮在循环中被声明为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 决定询问如何在循环中分配按钮功能。为此,您需要一个可以引用的函数列表,也就是 Selectors。

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

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

【讨论】:

  • 谢谢 即使按钮是通过循环创建的,如何为每个按钮分配自己的操作方法?因为我希望每个按钮执行不同的功能。
  • 那么您或许应该澄清您的问题,是吗?我会编辑我的答案
  • 添加了@OptiCode22
  • 如果您喜欢这个答案,请点赞 - 如果您认为它是“正确”的答案并且它解决了您的问题,请按此进行检查。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-21
  • 1970-01-01
  • 2017-02-06
  • 2016-09-02
  • 2018-07-30
相关资源
最近更新 更多