2010-10-04 15 views
5

Ecco il codice che sto utilizzando:Non è possibile ottenere a livello di codice fatto UIButton di lavorare - iPhone

-(void)viewDidLoad 
{ 
    NSString *checkerPath = [[NSBundle mainBundle] pathForResource:@"black-yellow-checker" ofType:@"png"]; 
    UIImage *checkerImage = [[UIImage alloc] initWithContentsOfFile:checkerPath]; 
    checkerView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 230.0f, 320.0f, 280.0f)]; 
    [checkerView setImage:checkerImage]; 

    UIButton *backButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain]; 
    backButton.frame = CGRectMake(45.0f, 175.0f, 230.0f, 50.0f); 
    [backButton setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"yellowButton" ofType:@"png"]] forState:UIControlStateNormal]; 
    [backButton addTarget:self action:@selector(goBackHome:) forControlEvents:UIControlEventTouchDown]; 
    [backButton setTitle:@"Back" forState:UIControlStateNormal]; 
    backButton.titleLabel.font = [UIFont fontWithName:@"Marker Felt" size:30]; 
    backButton.titleLabel.textColor = [UIColor blackColor]; 
    [checkerView addSubview:backButton]; 
} 

- (void)goBackHome:(id)sender 
{ 
    NSLog(@"go back pressed"); 
} 

Non capisco perché questo non funziona. Il pulsante appare, ma quando premo non succede nulla. Non cambia nemmeno nell'immagine "rientrata" quando la tocco. Niente. Non è possibile utilizzare IB, deve essere programmatico. qualche idea?

risposta

6

UIImageView has userInteractionEnabled set to NO by default. Ciò impedisce a qualsiasi subview di ottenere un tocco.

Si potrebbe abilitare interazione con l'utente della vista dell'immagine

checkerView.userInteractionEnabled = YES; 

o creare un nuovo UIView per includere sia checkerView e backButton.

UIView* newView = ... 
... 
[newView addSubview:checkerView]; 
[newView addSubview:backButton]; 
+0

AHA. pensavo che sarebbe stato di default. dal momento che sei così ben informato dovresti provarci http://stackoverflow.com/questions/3808087/where-to-find-what-mkdomainerror-error-error-4-means – Marty

3

Credo che forControlEvents: deve essere UIControlEventTouchUpInside.

+0

nah funziona anche con uicontroleventouchdown – Marty

Problemi correlati