gpt4 book ai didi

ios - 从编程 uibutton 属性访问全局变量

转载 作者:行者123 更新时间:2023-11-28 06:11:33 25 4
gpt4 key购买 nike

我想使用相同的 UIView 类来录制和播放声音。在这个 View 类中,我以编程方式添加了一个按钮,我想根据 View 的用途更改按钮文本。当 View 作为播放器创建时,它将显示播放图标(或现在的文本)。如果 View 创建为记录器,它将显示记录图标。但是我无法从我的 UIButton 访问 View 中的全局变量。

class AudioPlayerView: UIView {
var isRecorder = false

override init(frame: CGRect) {
super.init(frame: frame)

...

}
let theButton: UIButton = {
let button = UIButton()

if isRecorder {
button.setTitle("▷", for: .normal)
button.addTarget(self, action: #selector(playPauseAudio), for: .touchUpInside)
} else {
button.setTitle("▢", for: .normal)
button.addTarget(self, action: #selector(recordTapped), for: .touchUpInside)
}

button.backgroundColor = .clear
button.layer.cornerRadius = 15.0
return button
}()

最佳答案

我已经重新实现了你的播放器类。现在您不需要为录制和播放状态创建两个单独的 View 。您只需更改 isRecorder 值,您的按钮标题就会更改,而无需创建另一个 View 。

class AudioPlayerView: UIView {
var isRecorder = false {
didSet {
if isRecorder {
theButton.setTitle("▷", for: .normal)
} else {
theButton.setTitle("▢", for: .normal)
}
}
}

override init(frame: CGRect) {
super.init(frame: frame)
if isRecorder {
theButton.setTitle("▷", for: .normal)
} else {
theButton.setTitle("▢", for: .normal)
}
//Add your button to view as subview.
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)

}
let theButton: UIButton = {
let button = UIButton()
button.backgroundColor = .clear
button.layer.cornerRadius = 15.0
button.addTarget(self, action: #selector(tapButton), for: .touchUpInside)
return button
}()

func tapButton() {
if isRecorder {
playPauseAudio()
} else {
recordTapped()
}
}
func playPauseAudio() {

}
func recordTapped() {

}
}

关于ios - 从编程 uibutton 属性访问全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46390686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com