views:

1322

answers:

4

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.)

A: 

One little mistake I saw:

UIbutton *button = [[UIButton alloc] initWithFrame:CGRectMake(16.0, 20.0, 50.0, 50.0);
button.buttonType = UIButtonTypeRoundedRect;

add this to where is says:

 UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];

That mistake is causing the button to never even be allocated, also if it does not function you may need to put the button inside of a UIView. Hope this helps.

Jaba
isn't using: button.buttonType = UIButtonTypeRoundedRect; a read-only property? That's what the docs say.
samfu_1
After looking at this again, I see that you are saying to put the button inside a UIView. I'm confused by what you are saying because that's what I'm trying to do; that is, add a UIButton to a UIView. Was your sample code above a suggestion to fix it or a citation of my error? Thanks for any clarification you can provide.
samfu_1
I edited my answer, that should help you. I just wanted to let you know. Also what I ment by a UIView was the fact that you needed to add it to a UIView and not a UIImageView because a UIImageView is an image and when you put a button into an image it is only the image of the button and not the usability of a button. Add a UIView and make it animate with the UIImageView and add the UIButton into the UIView so that you still have the button and it is usable.
Jaba
Thanks for the clarification. The solution was to add my UIImageView to my UIView in the initWithFrame: method and then subsequently add all the buttons on top of it.
samfu_1
A: 

Are you loading your UIImageView subclass from a nib file? If you are, -initWithFrame: will not be called, but -initWithCoder: instead.

What I normally do is this:

- (void)didInit {
    // add your buttons here
}
- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self didInit];
    }
    return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self didInit];
    }
    return self;
}
Thomas Müller
I'm not using a nib file.. so that's not the problem.
samfu_1
Just make sure -initWithFrame actually gets called.
Thomas Müller
A: 

breakthrough: One problem I noticed was that in my view controller, I was using the initWithImage for instantiating my UIImageView instead of using initWithFrame: which was causing the image to be loaded, but not the button. Now the button appears but isn't functional.

view controller file now looks like this:

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

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

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

[super viewDidLoad];  
}

This allocates & initializes the frame which uses the overridden method from my UIIMageView subclass. Any help with getting the button to work would be greatly appreciated.

samfu_1
+1  A: 

I think that your problem here is that a UIImageView has its userInteractionEnabled property disabled by default. You could try just adding the line
customPad.userInteractionEnabled = true;
to the initialisation and be set.

Laughing_Jack