Pregunta

¡Esto es tan simple, estoy seguro! Estoy perdiendo algo y estoy agotado de intentar arreglarlo. Esperemos que alguien pueda ayudar.

El botón en CharacterView.m funciona pero el botón anidado en CharacterMale.m no lo hace. No estoy usando IB todo se hace de manera progresiva. ¿Qué haría que un botón funcione y otro no?

/////////////////////////////////////////////////////////////////////////////////
 CharacterController.m
/////////////////////////////////////////////////////////////////////////////////
#import "CharacterController.h"
#import "CharacterView.h"

@implementation CharacterController

- (id)init {
    NSLog(@"CharacterController init");
    self = [ super init ];
    if (self != nil) {
    }
    return self;
}

- (void)loadView {
    [ super loadView ];
    characterView = [ [ CharacterView alloc ] init];
    self.view = characterView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


- (void)dealloc {
    [super dealloc];
}

@end

/////////////////////////////////////////////////////////////////////////////////
 CharacterView.m
/////////////////////////////////////////////////////////////////////////////////

#import "CharacterView.h"
#import "CharacterMale.h"

@implementation CharacterView

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        characterMale = [ [ CharacterMale alloc ] init];
        [self addSubview: characterMale];

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 200, 200, 100);
        [button setImage:[UIImage imageNamed:@"btnCharSelect.png"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(ApplyImage:) forControlEvents:UIControlEventTouchUpInside];
        [ self addSubview: button ];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
}

-(void)ApplyImage:(id)sender
{
    NSLog(@"CharacterView button works");
}

- (void)dealloc {
    [super dealloc];
}

@end

/////////////////////////////////////////////////////////////////////////////////
 CharacterMale.m
/////////////////////////////////////////////////////////////////////////////////

#import "CharacterMale.h"
#import "CharacterController.h"

@implementation CharacterMale

- (id)init {
    self = [ super init];
    if (self != nil) {
        UIImage *image = [UIImage imageNamed:@"charMale.png"];
        imageView = [[ UIImageView alloc] initWithImage:image];
        [image release];
        [ self addSubview: imageView ];

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, 200, 100);
        [button setImage:[UIImage imageNamed:@"btnCharSelect.png"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(ApplyImage:) forControlEvents:UIControlEventTouchUpInside];
        [ self addSubview: button ];

    }
    return self;
}

-(void)ApplyImage:(id)sender
{
    NSLog(@"CharacterMal button works");
}

- (void)dealloc {
    [super dealloc];
}

@end
¿Fue útil?

Solución

FINALMENTE !!!!!!

Tuve que iniciar todas las vistas con initWithFrame y pasar rects de marco válidos. Init debe usarse con los controladores y initWithFrame pasando rects para UIViews !!!!

characterView = [ [ CharacterView alloc ] initWithFrame:CGRectMake(0, 0, 480, 320)]; 
then 
characterMale = [ [ CharacterMale alloc ] initWithFrame:frame];

Otros consejos

Otra cosa a considerar es que las subvistas UIButton que se han agregado a UIImageView no funcionan en absoluto. Debe crear un UIView al que agregue tanto la vista de imagen como los botones.

Probablemente esto se deba a que la interacción está desactivada de forma predeterminada en las vistas de imágenes, pero no lo he comprobado.

¿La nueva vista acepta la interacción del usuario? En otras palabras, userInteractionEnabled está habilitado en la vista " Caracteres " ;?

Tuve un problema similar cuando intenté agregar el botón durante la inicialización de una UIView con un marco de CGRectZero:

@implementation SomeView
...
- (id)initWithTitleImage:(UIImage *) newTitleImage {
  // BROKEN: frame with CGRectZero.
  self = [super initWithFrame:CGRectZero];
  if (self) {
    UIButton *someButton = ...;
    [self addSubview someButton];
  }
}

Una vez que cambié el marco a un rectángulo adecuado, el botón funcionó:

@implementation SomeView
...
- (id)initWithTitleImage:(UIImage *) newTitleImage {
  // WORKING: frame with proper CGRect.
  self = [super initWithFrame:CGRectMake(0, 0, 480, 320)];
  if (self) {
    UIButton *someButton = ...;
    [self addSubview someButton];
  }
}

He tenido suerte con un selector para un UIButton creado en drawRect utilizando UIControlEventTouchDown en lugar del popular UIControlEventTouchUpInside .

Para mí, el problema se debió a que el origen del marco del botón era más grande que el marco principal.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top