2010-03-24 2 views
0

J'ai créé un UIButton sur une vue et sur l'action de l'UIButton [UIButton * button1_obj] un autre bouton [UIButton * button2_obj] est créé sur la même vue.UIButton n'étant pas cliqué dans la programmation de l'iPhone

Je suis confronté à ces 2 problèmes ... s'il vous plaît guidez-moi comment procéder.

  1. button1_obj apparaît quand je lance mon projet, mais son pas cliqué ou mis en évidence, ni aucune exception est levée. Sur l'action de button1_obj, button2-obj doit apparaître sur la même vue, comment effacer la vue avant d'y placer button2_obj.

code pour la question (1):

-(void)start 

{ 

NSLog(@"--------------------------------------------------------------------"); 

NSLog(@"Entered CView start"); 

    //define size and position of view 
    subMainView_G_obj = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 480, 320)]; //initilize the view 
    //subMainView_G_obj.autoresizesSubviews = YES;    //allow it to tweak size of elements in view 

    UIButton_G_obj = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];  
    UIButton_G_obj.frame = CGRectMake(100,70, 150, 50); 
    UIButton_G_obj.backgroundColor = [UIColor clearColor]; 
    //UIButton_G_obj.adjustsImageWhenHighlighted = YES; 
    [UIButton_G_obj setTitle:@"UI" forState:UIControlStateNormal]; 
    [UIButton_G_obj addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside]; 
    [subMainView_G_obj addSubview:UIButton_G_obj]; 

    UIButton_G_obj = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];  
    UIButton_G_obj.frame = CGRectMake(100,150, 150, 50); 
    UIButton_G_obj.backgroundColor = [UIColor clearColor]; 
    UIButton_G_obj.adjustsImageWhenHighlighted = YES; 
    [UIButton_G_obj setTitle:@"Configuration" forState:UIControlStateNormal]; 
    [UIButton_G_obj addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside]; 
    [subMainView_G_obj addSubview:UIButton_G_obj]; 

    NSLog(@"Leaving CView start"); 
    NSLog(@"--------------------------------------------------------------------"); 
} 

- (void)action:(id) sender 
{ 

    NSLog(@"Inside action method.. On click of First View"); 
    buttonUIObj = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain]; 
    buttonUIObj.frame = CGRectMake(100,160,100,50); 
    [buttonUIObj setTitle:@"Next View" forState:UIControlStateNormal]; 
    buttonUIObj.backgroundColor = [UIColor clearColor]; 
    [subMainView_G_obj addSubview:buttonUIObj]; 

} 

j'ai déclarer UIButton_G_obj, buttonUIObj, subMainView_G_obj GLOBALEMENT.

Répondre

0

Difficile de savoir quel est le problème avec button1_obj, sans voir le code. Cependant, pour (2), vous voudrez probablement supprimer button1_obj en appelant [button1_obj removeFromSuperview].

+0

hey merci pour la solution (2) .. cela a fonctionné !! pour (1), j'ai mis à jour la question ci-dessus. – suse

+0

J'ai un problème dans la solution (2). Si je donne le code comme [Button1_obj removeFromSuperview] alors il enlève seulement un bouton. Je veux que toute la vue soit éclaircie et donne une nouvelle vue. Comment devrais-je faire ça ?? – suse

0

À propos du problème 1: Tout d'abord, vos deux boutons portent le même nom. Par conséquent, vous avez une fuite de mémoire parce que vous allouez de l'espace pour votre bouton deux fois. Utilisez des noms différents pour vos boutons.

A propos de problème 2: Vous pouvez cacher le bouton 2 jusqu'à ce que le bouton 1 est pressé: Dans -(void)start

UIButton_G_obj_1.tag = 1; 
... 
UIButton_G_obj_2.hidden = YES; 
UIButton_G_obj_2.tag = 2; 

Et dans la méthode d'action vous pouvez réafficher le bouton:

if (sender.tag == 1) { 
    UIButton_G_obj_2.hidden = NO; 
} 
Questions connexes