Problem adding subviews (UIButtons) to subclass of UIImageView

Posted by samfu_1 on Stack Overflow See other posts from Stack Overflow or by samfu_1
Published on 2009-12-23T22:39:01Z Indexed on 2010/05/07 15:18 UTC
Read the original article Hit count: 514

I'm adding a custom button/keypad to my application. Thus far, I have a UIImageView subclass that contains an animation that slides it from the bottom of the screen, and then back down when the user no longer needs it. I'm having trouble adding UIButtons to this UIImageView, however.

Since this is a subclass of UIView, I'm attempting to add buttons to my view via the initWithFrame: method. (slideDown method is the added animation)

in my UIImageView Subclass, I have a UIButton ivar object added:

-(id)initWithFrame:(CGRect)frame {

  if (self = [super initWithFrame: frame]) {
  UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
  button.frame = CGRectMake(16.0, 20.0, 50.0, 50.0);
  [button setTitle: @"Go" forState: UIControlStateNormal];
  [button addTarget: self action: @selector(slideDown) forControlEvents: UIControlEventTouchUpInside];
  self.button1 = button;
   [self addSubview: button1];
   NSLog(@"Button added");

}
return self;
}

In my view controller, I instantiate my UIIMageView Subclass in the -(void)viewDidLoad: method as follows:

-(void)viewDidLoad {
//other objects init'ed

ButtonPad *customPad = [[ButtonPad alloc] initWithImage: [UIImage imageNamed: @"ButtonPad.png"]];
customPad.frame = CGRectMake(0.0, 480.0, 320.0, 300.0);
self.buttonPad = customPad;

[self.view addSubview: buttonPad];
[customPad release];  

[super viewDidLoad];
}

My current app allows the view to slide up and down off of the screen without any problems. However, the button never appears. I have also tried adding the button to my buttonPad object by instantiating & adding it as a subView to the buttonPad in my view controller file. This worked... but it didn't allow the button to function.

I am wondering: A.) Is it appropriate to add buttons or any subview for that matter to the UIView initWithFrame: method or should I be adding these subviews as a subview to my buttonPad in the view Controller file? B.) Since I am creating a custom button/keypad, am i following a valid approach by using a normal UIViewController or should I be using something like a modal view Controller? ( I have little knowledge about these.)

© Stack Overflow or respective owner

Related posts about iphone

Related posts about cocoa-touch