gpt4 book ai didi

ios - 使用 touchesBegan Action 子类化 UIBarButtonItem

转载 作者:行者123 更新时间:2023-11-28 13:21:09 26 4
gpt4 key购买 nike

我正在尝试创建一个 UIBarButtonItem 的子类,我可以在 Storyboards/Xibs 中使用它,其中可以收听 touchesBegan Action (播放按钮声音就在触摸按钮时)。这是我得到的:

class UIAudibleBarButtonItem : UIBarButtonItem {
override init() {
super.init()
target = self
action = "touchesBegan:"
}

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

func touchesBegan(touches:NSSet, withEvent event:UIEvent) {
AudioManager.instance.playButtonSound()
}
}

但是它不起作用,即未调用 touchesBegan。我需要更改什么才能使这项工作正常进行?

更新:

class UIAudibleBarButtonItem : UIBarButtonItem {
override init() {
super.init();
setup();
}

override init(barButtonSystemItem systemItem: UIBarButtonSystemItem, target: AnyObject?, action: Selector) {
super.init(barButtonSystemItem: systemItem, target: target, action: action);
setup();
}

override init(image: UIImage?, landscapeImagePhone: UIImage?, style: UIBarButtonItemStyle, target: AnyObject?, action: Selector) {
super.init(image: image, landscapeImagePhone: landscapeImagePhone, style: style, target: target, action: action);
setup();
}

override init(image: UIImage?, style: UIBarButtonItemStyle, target: AnyObject?, action: Selector) {
super.init(image: image, style: style, target: target, action: action);
setup();
}


override init(title: String?, style: UIBarButtonItemStyle, target: AnyObject?, action: Selector) {
super.init(title: title, style: style, target: target, action: action);
setup();
}

override init(customView:UIView) {
super.init(customView: customView);
setup();
}

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


private func setup()
{
target = self;
action = "buttonPressed:";
println("setup()");
}


func buttonPressed(sender:AnyObject)
{
AudioManager.instance.playButtonSound();
println("buttonPressed()");
}
}

最佳答案

如果您使用 swift,这里有一个简单的方法来检测下面的操作。您所要做的就是在 Storyboard 中连接该类,单击您的栏按钮项目,您将看到 Action 触发。

稍后谢谢我!!!

class MVBarbuttonItem: UIBarButtonItem {

override func awakeFromNib() {
self.action = #selector(MVBarbuttonItem.didTapButton)
}

@objc func didTapButton() {

}

}

关于ios - 使用 touchesBegan Action 子类化 UIBarButtonItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25679629/

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