Domanda

Questo è così dannatamente semplice! Mi manca qualcosa e sono sfinito dal tentativo di risolverlo. spero che qualcuno possa aiutare.

Il pulsante in CharacterView.m funziona ma il pulsante nidificato in CharacterMale.m no. Non sto usando IB, tutto è fatto in modo programmatico. Cosa potrebbe far funzionare un pulsante e l'altro 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
È stato utile?

Soluzione

FINALMENTE !!!!!!

Ho dovuto avviare tutte le viste con initWithFrame e passare i rects di frame validi. Init dovrebbe essere usato con i controller e initWithFrame passando rects per UIViews !!!!

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

Altri suggerimenti

Un'altra cosa da considerare è che le sottoview UIButton a cui è stato aggiunto un UIImageView non funzionano affatto. Devi creare un UIView al quale aggiungere sia la vista immagine sia i pulsanti.

Ciò è probabilmente dovuto al fatto che l'interazione è disattivata per impostazione predefinita nelle viste delle immagini, ma non ho verificato

La nuova vista accetta l'interazione dell'utente? In altre parole, userInteractionEnabled è abilitato nella vista " Caratteri " ;?

Ho avuto un problema simile quando ho provato ad aggiungere il pulsante durante l'inizializzazione di una UIView con un frame di CGRectZero:

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

Dopo aver cambiato la cornice in un rettangolo corretto, il pulsante ha funzionato:

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

Ho avuto fortuna con un selettore per un UIButton creato in drawRect usando UIControlEventTouchDown invece del popolare UIControlEventTouchUpInside .

Per me il problema è stato causato perché l'origine della cornice del pulsante era maggiore della cornice principale.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top