ios - Swift Playgrounds UIButton 无法正常工作
问题描述
我在 swift 操场上有这个 UIButton 称为“开始按钮”。这是它的设置。
func setupStartButton() {
startButton.frame = CGRect(x: 15, y: 550, width: 125, height: 50)
startButton.backgroundColor = UIColor(ciColor: CIColor(red: 255/255, green: 0/255, blue: 77/255, alpha: 1.0))
startButton.layer.cornerRadius = 20
startButton.setTitle("Start", for: .normal)
startButton.setTitleColor(.white, for: .normal)
startButton.addTarget(self, action: #selector(startButtonTapped), for: .touchUpInside)
view.addSubview(startButton)
我在 viewDidLoad() 中调用它。
override func viewDidLoad() {
view.frame = CGRect(x: 0, y: 0, width: 524, height: 766)
view.backgroundColor = .white
setupStartButton()
PlaygroundPage.current.liveView = view
PlaygroundPage.current.needsIndefiniteExecution = true
}
但是当我点击一个按钮时,它不会运行名为“startButtonTapped”的函数,它只会将“Hello”打印到控制台。
这是函数'startButtonTapped'。
@objc func startButtonTapped() {
print("Hello")
}
为什么当我点击按钮时它什么也不做?我怎样才能解决这个问题?
谢谢
解决方案
我看不到你的其余代码。我只是快速创建了一个,以便您可以与自己的代码进行比较。
import UIKit
import PlaygroundSupport
class VC: UIViewController {
let startButton = UIButton(type: .system)
override func viewDidLoad() {
super.viewDidLoad()
setupStartButton()
}
func setupStartButton() {
startButton.frame = CGRect(x: 15, y: 550, width: 125, height: 50)
startButton.backgroundColor = UIColor(ciColor: CIColor(red: 255/255, green: 0/255, blue: 77/255, alpha: 1.0))
startButton.layer.cornerRadius = 20
startButton.setTitle("Start", for: .normal)
startButton.setTitleColor(.white, for: .normal)
startButton.addTarget(self, action: #selector(startButtonTapped), for: .touchUpInside)
view.addSubview(startButton)
}
@objc func startButtonTapped() {
print("Hello")
}
}
let vc = VC()
PlaygroundPage.current.liveView = vc
推荐阅读
- c - 将结构传递给函数以分配值
- python-3.x - 安装 kiteconnect 3.7.4 时无法卸载“chardet”
- batch-file - 如何使用 FFprobe 从 Webm 中提取持续时间
- javascript - Console.log 将数组转换为逗号分隔值
- javascript - ReactJS 和 javascript 未显示在 Rails 5 应用程序上
- ios - 如何绘制与 BarChartView 的条平行的文本?
- php - 错误“所有产品的价格必须一致”etsy
- parsing - 无法正确解析 tcpdump 输出
- macos - 在 macOS 上,我应该在哪里创建多版本场景中“设置默认”脚本的链接?
- java - 使用 AsyncTask 用多个项目填充 ExpandableListView