问题描述:

I create Start_button and make @IBOutlet and @IBAction

@IBOutlet weak var Start_button: UIButton!

@IBAction func Start_button(sender: AnyObject)

Now, i want hide button after click. I try this, but this don't work:

@IBAction func Start_button(sender: AnyObject)

{

Start_button.hidden = true;

}

Error message:

fatal error: unexpectedly found nil while unwrapping an Optional value

(lldb)

How i can hide this button?

Thanks for helping!

网友答案:

Its nil because you probably haven't connected it from your storyboard/nib. You need to connect the outlet, you can't just create an outlet in code and expect it to be connected to the visible element. The same goes for your action. @IBOutlet / @IBAction stands for Interface Builder Outlet/Action, which means you have to connect them in Interface Builder.

Also its better if your action uses the sender, and not a local variable (when its pointing to the same thing). And you shouldnt use ;at the end of the line.

@IBAction func Start_button(sender: UIButton) // Change to UIButton
{
    sender.hidden = true
    // OR
    // (sender as! UIButton).hidden = true
}
网友答案:
@IBAction func button_nameA(sender: AnyObject) {
// show hidden buttons
    self.Target_Object.hidden = false

}

So when you click the A buttons its automaticly send to you target and in target also you have to hide button if you working single view app

相关阅读:
Top