2016-05-18 3 views
1

UIActionSheet, il a des boutons avec titre, je vais chercher le titre de la matrice .Je veux obtenir le titre des boutons et l'affichage en UILabel, que je l'ai fait, mais si j'appuie sur le bouton annuler le annuler les boutons aussi l'affichage, je ne veux pas afficher le titre du bouton annuler UILabel le code ci-dessous que je l'ai essayé,UIActionSheet Annuler le bouton ne fonctionne pas correctement

- (IBAction)site_Selection:(id)sender { 

NSArray *array = @[@"one",@"two",@"three",@"Smart Gladiator1"]; 

UIActionSheet *actionSheet = [[UIActionSheet alloc] 
           initWithTitle:nil 
           delegate:self 
           cancelButtonTitle:nil 
           destructiveButtonTitle:nil 
           otherButtonTitles:nil]; 
actionSheet.delegate = self; 
for (NSString *title in array) { 
    [actionSheet addButtonWithTitle:title]; 
} 

actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:@"Cancel"]; 


[actionSheet showInView:self.view]; 


    } 

    -(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ 

NSString *title = [actionSheet buttonTitleAtIndex:buttonIndex]; 


self.btn_site_selection.titleLabel.text = [actionSheet buttonTitleAtIndex:buttonIndex]; 

} 

S'il vous plaît aidez-moi à ce faire,

Répondre

1

Vous ne devriez pas gérer le bouton d'annulation presse.

Comme tous les boutons sur UIActionSheet sont gérés par actionSheet:clickedButtonAtIndex:, vous devrez vérifier si l'index du bouton est celui du bouton d'annulation. Vous pouvez le faire avec le cancelButtonIndex sur UIActionSheet:

- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ 
    if (actionSheet.cancelButtonIndex == buttonIndex) { 
     return; 
    } 

} 
+0

@rckoenes Merci, il fonctionne bien – user6183984

0

Vous pouvez essayer cette

- (IBAction)actionSheetCall:(id)sender { 


    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Confirm call" 
                    message:@"Are you sure to call?" 
                  preferredStyle:UIAlertControllerStyleActionSheet]; // 1 

    UIAlertAction *secondAction = [UIAlertAction actionWithTitle:@"Cancel" 
                  style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) { 
                   NSLog(@"You pressed cancel button "); 
                  }]; // 3 


    for(int i = 0; i < [Arr_phone count]; i++) { 

     UIAlertAction *firstAction = [UIAlertAction actionWithTitle:[activity.Arr_phone objectAtIndex:i] 
                   style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { 
                    NSLog(@"You pressed Logout one"); 
                    //[self setupCall]; 
                    [self incomingCall:[Arr_phone objectAtIndex:i]]; 
                   }]; // 2 
     [alert addAction:firstAction]; // 4 

    } 

    [alert addAction:secondAction]; // 5 

    [self presentViewController:alert animated:YES completion:nil]; // 6 


}