0

我想在 iPad 的 Swift Playground 中制作一个真心话大冒险游戏,但我无法制作一个按钮来打印多个敢于或真心话。我试图制作一个具有所有胆量的 var,然后将其放入(类 Responser NSObject)中,该 var 看起来像这样(vardares = ["dare1", "dare 2" ...]) 但它给了我错误说(无法将类型'[String]'的值分配给类型'String')和我尝试的第二件事是(让敢= [“敢1”,“敢2”...])给了我同样的错误. 我的代码看起来像这样


import PlaygroundSupport
import UIKit

let View = UIView()
View.backgroundColor = .blue
let lbl = UILabel(frame: CGRect(x: 210, y: 329, width: 200, height: 50))
lbl.text = "hi!"
View.addSubview(lbl)
lbl.textColor = .white


let button = UIButton(frame: CGRect(x: 67, y: 500, width: 100, height: 50))
button.backgroundColor = .green
button.setTitle("button", for: UIControl.State.selected)
button.layer.cornerRadius = 10
View.addSubview(button)

let button1 = UIButton(frame: CGRect(x: 300, y: 500, width: 100, height: 50))
button1.backgroundColor = .red
button1.setTitle("button1", for: UIControl.State.selected)
button1.layer.cornerRadius = 10
View.addSubview(button1)

class Responser: NSObject

{
    //Method to be called

    @objc func printname()

    {
        lbl.text = "1"

    }

}

let responder = Responser()
button.addTarget(responder, action: #selector(Responser.printname), for:.touchUpInside)  

class Responser1: NSObject

{
    //Method to be called

    @objc func printname()
    {
        lbl.text = "2"
    }

}

let responder1 = Responser1()
button1.addTarget(responder1, action: #selector(Responser1.printname), for:.touchUpInside)
PlaygroundPage.current.liveView = View

4

0 回答 0