gpt4 book ai didi

iphone - 应用程序重新启动时无法按下以编程方式生成的按钮

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

我在我的应用程序中以编程方式生成了一系列按钮,按下它们时它们可以完美工作。然后我将按钮的数量存储在内存中,当我重新启动应用程序时,按钮会像以前一样重新生成,但无法按下。当按下按钮时,“deletebtn”方法会打印“pressed”,但事实并非如此。我什至尝试添加以编程方式生成的按钮“buttonName.userInteractionEnabled = true”的属性,但没有任何变化。

这是按钮的属性,这也是我在 viewdidload 上的代码,它会重新创建与我存储在内存中的按钮数量一样多的按钮:

let btn: UIButton = UIButton(frame: CGRectMake(10, CGFloat(60*a), ScreenWidth - 20 , 50))    
btn.layer.cornerRadius = 0.5 * btn.bounds.size.height
btn.backgroundColor = UIColor.whiteColor()
btn.setTitleColor(UIColor.blackColor(), forState: .Normal)
btn.setTitle(ArrayService[(index)], forState: UIControlState.Normal)
btn.addTarget(self, action: Selector("deletebtn:"), forControlEvents: UIControlEvents.TouchUpInside)
btn.tag = a //an int variable that is increased each time i create a neew button
btn.userInteractionEnabled = true
self.ScrollPwd.addSubview(btn)

这是方法deletebtn:

func deletebtn(sender:UIButton){
print("pressed")
}

scrollView 是我放置按钮的地方。我尝试添加“ScrollPwd.userInteractionEnabled = true”,但我只能滚动按钮,但无法按下它们。

你能帮我吗?

最佳答案

我认为最大的问题是你如何定义 Action 。这是更新版本(Swift 3)

let btn: UIButton = UIButton(frame: CGRect(x: 10, y: 60 * indexButton, width: screenWidth - 20, height: 50))
btn.layer.cornerRadius = 0.5 * btn.bounds.size.height
btn.backgroundColor = UIColor.white
btn.setTitleColor(UIColor.black, for: .normal)
btn.setTitle(arrayService[indexButton], for: .normal)
btn.addTarget(self, action: #selector(self.deletebtn), for: .touchUpInside)

btn.tag = indexButton
self.scrollPwd.addSubview(btn)

我忍不住更改(修复)您的一些变量名称。您应该尽量避免将变量名称大写,因为这是为类名称保留的 - 所以 ScreenWidth 实际上应该是 screenWidth,并且当您已经有一个变量 index,您不需要一个相同的、随机命名的变量 a 来保存相同的信息。另外,indexButton 告诉我们索引的用途

关于iphone - 应用程序重新启动时无法按下以编程方式生成的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41964682/

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