Iphone UIButton não funciona no UIViews aninhados
-
03-07-2019 - |
Pergunta
Isto é tão maldito simples im certeza! Im faltando alguma coisa e estou exausto de tentar corrigi-lo. espero que alguém pode ajudar.
O botão no CharacterView.m funciona, mas o botão aninhado para baixo em CharacterMale.m não. Não estou usando o IB tudo é feito progmatically. O que poderia causar um botão para o trabalho e outro não?
/////////////////////////////////////////////////////////////////////////////////
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
Solução
FINALMENTE !!!!!!
Eu tive que o init todos os pontos de vista com initWithFrame e passar rects quadro válidos. Init deve ser usado com os controladores e initWithFrame passando rects para UIViews !!!!
characterView = [ [ CharacterView alloc ] initWithFrame:CGRectMake(0, 0, 480, 320)];
then
characterMale = [ [ CharacterMale alloc ] initWithFrame:frame];
Outras dicas
Outra coisa a considerar é que subviews UIButton
que foram adicionados a UIImageView
não funcionam em tudo. Você precisa criar um UIView
que você adicionar tanto o ponto de vista de imagem e os botões para.
Este é provavelmente porque a interação é desligada por padrão em vista de imagem, mas eu não ter verificado isso.
Será que a nova visão aceitar interação do usuário? Em outras palavras, é userInteractionEnabled habilitado nos "Personagens" Vista?
Eu tive um problema semelhante quando tentou adicionar o botão durante a inicialização de um UIView com um quadro de CGRectZero:
@implementation SomeView
...
- (id)initWithTitleImage:(UIImage *) newTitleImage {
// BROKEN: frame with CGRectZero.
self = [super initWithFrame:CGRectZero];
if (self) {
UIButton *someButton = ...;
[self addSubview someButton];
}
}
Depois que eu mudei o quadro para um retângulo adequada, o botão funcionou:
@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];
}
}
Eu tive sorte com um seletor para um UIButton criado em drawRect usando UIControlEventTouchDown
vez do UIControlEventTouchUpInside
popular.
Para mim, o problema foi causado porque, a origem do quadro de botão era maior então o quadro pai.