无法隐藏UIButton?

时间:2016-01-03 11:49:29

标签: ios swift uibutton

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

我已经耗尽了我有限的知识,并且在3小时的大部分时间里都在查阅Apple的文档和互联网,我终于来到了这里。我现在已经学习了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
}

}

4 个答案:

答案 0 :(得分:1)

你的代码毫无意义。正如@formal在他的回答中所说,你在每个水龙头上都创建了一个新按钮,这是错误的。

您想在故事板中定义按钮。

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

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

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

答案 1 :(得分:1)

您的主要问题是每次按下按钮时都会创建一个新按钮。为你的按钮创建一个@IBOutlet,只需在butPressed中设置它的隐藏属性(可以设置为按钮的动作)。

类似的东西:

class ViewController: UIViewController {

    @IBOutlet weak var button: UIButton!
    @IBOutlet weak var buttonMessageDisplay: UILabel!

    var tapcount = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }


    @IBAction func butPressed(sender: AnyObject) {

        switch tapcount {

        case 19...23:
            //Hides the button
            button.hidden = true

        case 24...31:
            //Unhides the button
            button.hidden = false

        default: break
        }

        print("Tap Count: \(tapcount)")
        buttonMessageDisplay.text = "Tap: \(tapcount)"

        ++tapcount

    }

}

答案 2 :(得分:0)

方法buttonPressed()每次调用时都会创建一个新按钮。您应将button定义为与buttonMessageDisplay类似的属性,并将代码置于viewDidLoad()内进行初始化。

答案 3 :(得分:-2)

你应该在案件条件范围内给出空格:

例如:

1