-1

I have a class which includes a UIButton as a subview. I have a subclass of UIButton which I've made, which has my own modifications to it, and I want to use this in place of the UIButton, but I don't want to just remove the original UIButton and add this one on instead, because then it wouldn't respond to the UIButton commands that the class gives to it.

How can I use my subclass of UIButton here?

Hope this makes sense.

EDIT: So I have a subclass of UIView. It's called MyView. MyView has a subview of a UIButton. I also have a subclass of UIButton, called MyButton. I want to use MyButton instead of UIButton on MyView. How can I do this?

Andrew
  • 15,935
  • 28
  • 121
  • 203
  • 4
    Are you saying you have created a new class something like: MyButton : UIButton??? And if so, you want to use MyButton instead of UIButton??? If that's the case, then you would just alloc and init your MyButton instead of UIButton??? – El Guapo Jul 09 '12 at 16:50
  • @Andrew if its in IB you can set the class of anything to a subclass, so find the panel where you can set your button class to MyButton – Justin Meiners Jul 09 '12 at 16:54
  • @JustinMeiners I'm not using IB. – Andrew Jul 09 '12 at 16:54
  • 1
    @Andrew then like ElGuapo said say [[MyButton alloc] init]; instead of [[UIButton alloc] init] – Justin Meiners Jul 09 '12 at 16:55
  • Are you subclassing `UIButton` to do custom drawing? or just to add properties? – Joe Jul 09 '12 at 16:59
  • 1
    «because then it wouldn't respond to the UIButton commands that the class gives to it.» Yes, it will. Anything that you don't override will use the superclass (`UIButton`)'s implementation. – jscs Jul 09 '12 at 17:23
  • 1
    @Andrew, I believe your problem here goes beyond simply extending the functionality of the UIButton, it seems you don't really understand the concepts of inheritance and polymorphism in regards to OOP. I would also suggest reading up on that a little. – 8vius Jul 09 '12 at 18:47

2 Answers2

4

At top of header or implementation file:

#import "MyView.h"
#import "MyButton.h"

Where you need it:

MyView *myView = [[MyView alloc] init];
MyButton *myButton = [[MyButton alloc] init];
[myView addSubview:myButton];

Simply as that, of course setting the button to be at the right place, having the right size, etc, I leave up to you.

8vius
  • 5,786
  • 14
  • 74
  • 136
  • Ah, but I want the properties on the button to change as they would if it were actually in the place of the UIButton, ie the title. – Andrew Jul 09 '12 at 17:19
  • 2
    If you have properties like that in your custom button class you need to declare them in the header file and not in the implementation as to be able to exposed them to the other classes where you access them. And if you're extending UIButton your custom button will have all the same properties accessible in the same manner as UIButton. – 8vius Jul 09 '12 at 17:20
  • You should look to @Joe's answer as well, if all your doing is adding properties it is a better approach. – 8vius Jul 09 '12 at 18:45
1

If you are subclassing UIButton just to add properties or methods then you should create a category instead. If you are creating a custom button that needs to override existing methods or perform additional drawing then you just need to create an instance of your button and add it to your view.

MyButton *button = [[MyButton alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
Community
  • 1
  • 1
Joe
  • 56,979
  • 9
  • 128
  • 135
  • I was about to post the same thing, but since @Joe was the one who educated me on it too, he should get the credit. Here's a previous thread on the subject: http://stackoverflow.com/questions/10193908/create-a-custom-uibutton-class-with-delete-function/10194181#10194181 – strings42 Jul 09 '12 at 16:57
  • I know that, but the problem is that it already has the UIButton, it's init'd, and I want to add all of it's properties to this MyButton that I have. – Andrew Jul 09 '12 at 19:16
  • Use a [category](http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectivec/chapters/occategories.html) if the button is already initialized. Use the link I included in the answer if you need *instance* variables to back properties. – Joe Jul 09 '12 at 19:55