gpt4 book ai didi

ios - 如何调用ViewController中所有自定义UIView的方法?

转载 作者:行者123 更新时间:2023-11-30 13:35:12 24 4
gpt4 key购买 nike

我创建了一个自定义 UIView,里面有一个 UIButton当我点击它时,它简单地改变了它的背景颜色。

我想知道如何更改此自定义 UIView 的所有实例中此按钮的背景颜色。

有什么想法吗?

import UIKit

class RadioChannelView: UIView {

var playButton:UIButton!

init(frame: CGRect, themeColor: UIColor) {
super.init(frame: frame)

playButton = UIButton(type: .Custom)
playButton.backgroundColor = UIColor.blackColor()
playButton.layer.cornerRadius = 40

playButton.addTarget(self, action: "playButtonTapped", forControlEvents: .TouchUpInside)

}

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

func playButtonTapped() {
self.playButton.backgroundColor = UIColor.redColor()
}

}

最佳答案

其中一种方法是将 RadioChannelView 类添加为 NSNotificationCenter 的观察者。单击按钮将您的通知发布给观察者,反过来它会唤醒该通知的监听器方法,您可以在其中更改背景。

class RadioChannelView: UIView {

var playButton:UIButton!

init(frame: CGRect, themeColor: UIColor) {
super.init(frame: frame)

playButton = UIButton(type: .Custom)
playButton.backgroundColor = UIColor.blackColor()
playButton.layer.cornerRadius = 40

playButton.addTarget(self, action: "playButtonTapped", forControlEvents: .TouchUpInside)

NSNotificationCenter.defaultCenter().addObserver(self, selector: "didReceiveBtnClickNotification:", name: "button_clicked", object: nil)
}

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

func playButtonTapped() {
NSNotificationCenter.defaultCenter().postNotificationName("button_clicked", object: nil);
}

func didReceiveBtnClickNotification(sender: NSNotification!) {
self.playButton.backgroundColor = UIColor.redColor()
}
}

关于ios - 如何调用ViewController中所有自定义UIView的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36162869/

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