0

I'm creating a keyboard extension in XCode Beta 7 (Swift2).

I'm trying to connect multiple buttons to a single action.

However, when I connect all the button outlets to this action, it causes my keyboard to crash which makes me think there's something I'm missing.

@IBOutlet weak var Label: UIButton!


@IBAction func ButtonTapped(sender: AnyObject) {

    Label.setTitle("Hello!", forState: .Normal)
    Label.hidden = false
    Label.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.5)

    NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: "hideLabel", userInfo: nil, repeats: false)
}

@objc func hideLabel() {
    Label.hidden = true
    Label.backgroundColor = UIColor.clearColor()

}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Cate
  • 33
  • 6

1 Answers1

1

Attaching the same action to multiple buttons is not a problem, and is a common technique.

However, you have Label set as force-unwrapped, which will crash if it is nil.

My guess is that your Label outlet is not connected so Label is nil at runtime.

Change the type of Label from UIButton! to UIButton? and then change your code to use "if let" optional binding to only execute the code using Label if label is non-nil.

BTW, Swift has a strong naming convention. Variable names should start with a lower-case letter and use "camel case" for the rest of the variable name. Only class names and type names should start with a capital letter. So "Label" should be "label" instead. Get in the habit of following that style. It serves as a clear visual cue to you and others reading your code as to what type of thing a name refers to.

Finally, "Label" is a horrible name for a button. It's a button, not a UILabel. Call it "myButton" or something instead.

EDIT

The code might look like this:

@IBOutlet weak var myButton: UIButton?


@IBAction func ButtonTapped(sender: AnyObject) 
{
  println("myButton = \(myButton)")  //See if myButton is nil
  if let myButton = myButton
  {
    myButton.setTitle("Hello!", forState: .Normal)
    myButton.hidden = false
    myButton.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.5)

    NSTimer.scheduledTimerWithTimeInterval(2, target: self, 
      selector: "hidemyButton", 
      userInfo: nil, repeats: false)
    }
}

@objc func hidemyButton() 
{
    myButton.hidden = true
    myButton.backgroundColor = UIColor.clearColor()

}
Community
  • 1
  • 1
Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Thank you! Can you give me a little more information on what "change your code to use "if let" optional binding to only execute the code using Label if label is non-nil." looks like? Like I said, I'm such a noob at this and still learning the basics. :) Thank you again!!! – Cate Aug 06 '15 at 16:08
  • 1
    I just edited my post to provide example code. I took pity on you because you're new. Normally we frown on "show me the code" type questions, and ask you to do your own research first. – Duncan C Aug 06 '15 at 16:24
  • Thank you -- forgive me, I'll keep that in mind for the future. However, after implementing the code and connecting the outlets, the keyboard is still crashing whenever I click on a button. – Cate Aug 06 '15 at 17:41
  • So post the specifics of your crash. Which line is crashing. What is the exact error message you are getting? – Duncan C Aug 06 '15 at 17:59
  • I am not getting any error codes, but when I build the app and simulate on my IOS device, the keyboard crashes whenever I click on the button associated with "ButtonTapped". – Cate Aug 06 '15 at 18:06
  • You must be getting a message when you crash. You're making it impossible to get enough information to help you. I give up. – Duncan C Aug 06 '15 at 18:18
  • I'm sorry to confuse you, I'm not sure how else I can explain this -- the app builds fine, but when I simulate on my IOS device, open the iMessage app, navigate to the custom keyboard I've built, and click on a button (the button associated with the "ButtonTapped" action in the coding), the keyboard goes blank then skips to the Apple IOS keyboard. – Cate Aug 06 '15 at 18:23