开发者

Problem adding subviews (UIButtons) to subclass of UIImageView

开发者 https://www.devze.com 2022-12-14 22:50 出处:网络
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 n

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开发者_JS百科");

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


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.


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.


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;
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消