gpt4 book ai didi

ios - 无法隐藏 UIButton?

转载 作者:搜寻专家 更新时间:2023-11-01 05:37:58 25 4
gpt4 key购买 nike

我试图让自定义 UIButton 在按下一定次数后隐藏起来......但我不知所措。

在耗尽了我有限的知识并查阅了 Apple 的文档以及 3 个小时的互联网之后,我终于来到了这里。我学习 Swift 已经有一段时间了,并且正在努力变得更加熟悉它。这是我的第一个面向对象语言,至少可以说它在考验我。非常感谢您对这个很可能是荒谬的简单问题的任何帮助。

import UIKit

class ViewController: UIViewController{

@IBOutlet weak var buttonMessageDisplay: UILabel!

override func viewDidLoad() {
super.viewDidLoad()
buttonPressed()
}

var tapcount = 0

let buttonMessage : [String] = [/* long array of strings */]

func buttonPressed() {

let button = UIButton(type:.Custom) as UIButton
button.frame = CGRectMake(0, 0, 100, 100)
button.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
button.backgroundColor = UIColor.redColor()
button.layer.borderColor = UIColor.blackColor().CGColor
button.layer.borderWidth = 3
button.layer.cornerRadius = 0.5 * button.bounds.size.width
button.setTitle("", forState: UIControlState.Normal)
button.addTarget(self, action: "buttonPressed", forControlEvents: .TouchUpInside)
view.addSubview(button)


switch tapcount {

case 19...23:
//Hides the button
button.hidden = true
buttonMessageDisplay.text = buttonMessage[tapcount]

case 24...31:
//Unhides the button
button.hidden = false
buttonMessageDisplay.text = buttonMessage[tapcount]

default:
buttonMessageDisplay.text = buttonMessage[tapcount]
}

print("Tap Count: \(tapcount)")
++tapcount

}

更新了手势识别器:

import UIKit

class ViewController: UIViewController{

@IBOutlet weak var buttonMessageDisplay: UILabel!

override func viewDidLoad() {
super.viewDidLoad()

buttonMessageDisplay.text = ""

let button = UIButton(type:.Custom) as UIButton
button.frame = CGRectMake(0, 0, 100, 100)
button.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
button.backgroundColor = UIColor.redColor()
button.layer.borderColor = UIColor.blackColor().CGColor
button.layer.borderWidth = 3
button.layer.cornerRadius = 0.5 * button.bounds.size.width
button.setTitle("", forState: UIControlState.Normal)
button.addTarget(self, action: "buttonPressed", forControlEvents: .TouchUpInside)
self.view.addSubview(button)
}


var tapcount : Int = 0

let buttonMessage : [String] = [/* array of strings */]

@IBAction func userTap(sender: UITapGestureRecognizer) {
print("Tap Received")
if case 19...23 = tapcount {
buttonPressed()
}
}



func buttonPressed() {

switch tapcount {

case 0...18:
buttonMessageDisplay.text = buttonMessage[tapcount]

case 19...23:
//Hides the button
button.hidden = true
buttonMessageDisplay.text = buttonMessage[tapcount]

case 24...32:
//Unhides the button
button.hidden = false
buttonMessageDisplay.text = buttonMessage[tapcount]

case 33...100:
buttonMessageDisplay.text = buttonMessage[tapcount]

default:
print("There are no more messages or an error has been encountered")
}

print("Tap Count: \(tapcount)")
++tapcount
}

最佳答案

您的代码毫无意义。正如@formal 在他的回答中所说,您在每次点击时都创建了一个新按钮,这是错误的。

您想在 Storyboard 中定义按钮。

然后您需要一个 IBAction 方法,它将按钮作为参数:

@IBAction func buttonPressed(sender: UIButton) 
{
++tapcount
if tapcount < 19
{
sender.hidden = true
}
}

请注意,如果您要隐藏的按钮与用户正在点击的按钮相同,那么一旦它被隐藏,您就完成了。用户无法点击隐藏按钮,因此无法取消隐藏。 (因此你的 switch 语句没有意义)

关于ios - 无法隐藏 UIButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34576207/

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