2010-12-09 51 views
1

Je suis nouveau à l'objectif C. J'ai créé la méthode pour construire & afficher un bouton dans une UIView (UIView est dans un autre vue nommée contentView et contentView à ajouter en tant que sous-vue dans un UIScrollView. Eh bien, le problème est que je ne peux pas cliquer sur le bouton généré pour déclencher l'action IB nommé playAction. quelqu'un peut-pls me aider? MerciNe peut pas déclencher IBAction d'un UIButton de UIView

- (void) displayCategoryBestSellers 
{ 
    //Structure: ScrollView -> ContentView -> MainView -> ImageView and Button 

    UIView * mainView = [[UIView alloc] initWithFrame:CGRectMake(0,0,160,105)]; 
    mainView.userInteractionEnabled = YES; 

    UIImage * backgroundImage = [UIImage imageNamed:@"blackwhitesquare.png"];   
    UIImageView * uiImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,145,105)]; 
    uiImageView.image = backgroundImage; 
    uiImageView.center = CGPointMake(mainView.frame.size.width /2, mainView.frame.size.height/2); 
    uiImageView.userInteractionEnabled = YES; 

    [mainView addSubview:uiImageView]; 

    UIButton *playButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain]; 
    playButton.frame = CGRectMake(0,0,100,90); 

    NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://media.theseus.cataloguesolutions.com/images/72by72/324178611_0004_PG_1.jpg"]]; 
    UIImage *myimage = [[UIImage alloc] initWithData:mydata]; 

    [playButton setBackgroundImage:myimage forState:UIControlStateNormal]; 
    playButton.center = CGPointMake(mainView.frame.size.width /2, mainView.frame.size.height/2);   
    [playButton addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchDown];  
    playButton.userInteractionEnabled = YES; 

    [mainView addSubview:playButton]; 

    [contentView addSubview:mainView]; 

    contentView.userInteractionEnabled = YES; 

    [scrollView addSubview:contentView]; 
    scrollView.delegate = self; 

    scrollView.userInteractionEnabled = YES;   
} 

-(IBAction)playAction: (id)sender 
{ 
    NSLog(@"Button Clicked"); 
} 
+0

est-ce que le tour de bouton bleu w quand vous appuyez dessus? – Brian

Répondre

1

utilisation UIControlEventTouchUpInside au lieu de UIControlEventTouchDown.

+0

J'ai essayé votre suggestion mais toujours le même problème. S'il vous plaît, aidez quelqu'un parce que j'ai vraiment besoin de ça pour travailler. – Lilz