-2

I want to create my own custom button without using any interface builder.

Actually i wants to set all my properties/attribute(Like frame,color,size,label etc.) separate in a NSObject class.Then from my UIView class i want to draw them .

How can i do it as i am never use NSObject class?

Any sample application or example to do this?

user930195
  • 432
  • 1
  • 5
  • 19
  • 1
    If you want to create a custom button you should at least inherit from `UIView` (if you don't wish to inherit from `UIButton` directly). Making it a (direct) subclass of `NSObject` will just complicate things. – alex-i Sep 30 '11 at 08:21

2 Answers2

3

Actually, you can subclass UIButton - for an example, have a look at Fun With UIButtons and Core Animation Layers. Is this is too much, you can simply create a new UIButton and set it's properties:

UIButton *btn = [[UIButton alloc] initWithFrame:yourFrame]
btn.backgroundColor = [UIColor redColor];
...
[btn release;]
tilo
  • 14,009
  • 6
  • 68
  • 85
1

Check this Code:

    UIButton *btn=[[UIButton alloc] initWithFrame:CGRectMake(152,0,150,128)];       
    [btn setBackgroundColor:[UIColor clearColor]];
    [btn setImage:actualImage forState:UIControlStateNormal];   
    [btn setTag:1];
    [btn addTarget:self action:@selector(btnTouched:)forControlEvents:UIControlEventTouchDown];
    [self addSubview:btn];
    [btn release];
rptwsthi
  • 10,094
  • 10
  • 68
  • 109
  • 1
    you should clean up your code in order to avoid confusion, e.g. there is no `i` declared, you are adding btn as a subview to btn, there is no `copyOf_myGlobalArrayOfImageIds` and you should add `[btn release];` at the end of your code. – tilo Sep 30 '11 at 08:26
  • 1
    you're over releasing it now :) (you autorelease it in the first line and release it again in the last line) – alex-i Sep 30 '11 at 09:00
  • Now it's fine i though :) thanks @babbidi – rptwsthi Sep 30 '11 at 09:14