gpt4 book ai didi

ios - Swift UIButton addTarget 自动启动

转载 作者:行者123 更新时间:2023-11-29 01:23:28 24 4
gpt4 key购买 nike

我在 UIButton 中的 addTarget 有点问题。当我运行我的应用程序时,我的功能会自动启动,而他需要按 UIButton 才能启动。我不知道我的问题在哪里。

我的按钮

let BTN_happy : UIButton = UIButton()
BTN_happy.frame = CGRectMake(0, rowHeight * 1, fullWidth, rowHeight)
BTN_happy.backgroundColor = UIColor(hexString: "#ff3b30")
BTN_happy.addTarget(self, action: Selector(data("Happy")), forControlEvents: .TouchUpInside)

self.view.addSubview(BTN_happy)
text("Happy", button: BTN_happy)

我的函数

func text(text: String, button: UIButton) {

let txt : UILabel = UILabel()
txt.text = text.uppercaseString
txt.frame = CGRectMake(20, 0, fullWidth - 40, button.bounds.size.height)
txt.textColor = UIColor(hexString: "#ffffff")
txt.font = UIFont.systemFontOfSize(30, weight: UIFontWeightHeavy)
txt.shadowOffset = CGSize(width: 2, height: 2)
txt.shadowColor = UIColor(hexString: "#000000", alpha: 0.3)

button.addSubview(txt)

}

func data(mood: String) {
NSLog("Mood: \(mood)")
}

预先感谢您的回复。

最佳答案

您不能像那样添加选择器。您需要将其更改为:

myBTN.addTarget(self, action: Selector("data:"), forControlEvents: .TouchUpInside)

同时更改方法签名,如:

func data(sender: UIButton)
{
NSLog("Button clicked")
}

关于ios - Swift UIButton addTarget 自动启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34317576/

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