0

I have a class MyButton : UIButton which inherits from UIButton. I do a bunch of things in the initWithFrame (the only constructor)of MyButton say like setting the backgroundcolor.

Now I want to use this MyButton in my xib. so that I dont keep setting these properties again and again for all my buttons. I have also set the Custom Class to MyButton in the Identity Inspector for the button in the xib.

Nothing still reflects the properties I set in the xib. This could have been easily done if it was in the code.

My question is, 1) What gets called when you create button thru a xib (like you call initWithFrame when you programmatically create a button) ?

2) How do I get it to see the properties I set in the MyButton ? Is moving out of xib and doing it programatically the only way ?

thanks in advance !

FatalError
  • 574
  • 1
  • 7
  • 18

2 Answers2

1
  1. Typically with initWithCoder:
  2. You can use the identity inspector in Interface Builder to set values using the keypath of the attributes. In this example, you can change the CALayer properties of the view:

enter image description here

Wayne Hartman
  • 18,369
  • 7
  • 84
  • 116
  • For point number 2, I will have to keep doing for every MyButton I create. That destroys the point of having a custom control. – FatalError Mar 26 '14 at 16:15
  • btw your initWithCoder comment was helpful. I could define the properties in there ! Thanks !! Marking as answer – FatalError Mar 26 '14 at 18:58
  • one more thing - initWithCoder will not pick up the values defined in the Key Path. The values does get picked up in AwakeFromNib though. – FatalError Mar 26 '14 at 20:41
0

You can simply set it in your xib file. For that:

  1. Select the button in the xib file.
  2. Click the Identity Inspector button.
  3. Type the name of your custom class(MyButton for your case) in the Class field.

Now your button will be with the Type MyButton class.

EDIT

Check Apple's documentation on Adding a Custom Object. This will give you more idea.

Sujith Thankachan
  • 3,508
  • 2
  • 20
  • 25
  • As I said it doesn't pick up the properties I set in the base class. I want to use the MyButton custom control which has some properties set directly from the Xib. – FatalError Mar 26 '14 at 16:13