2016-05-18 78 views
1

UIActionSheet,它有按鈕標題,我從數組中取出標題。我想獲得按鈕標題並顯示在UILabel,我做了,但如果我按取消按鈕取消按鈕還顯示,我不想在UILabel 下面的代碼,我試圖顯示取消按鈕標題,UIActionSheet取消按鈕無法正常工作

- (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]; 

} 

請幫我做這件事,

回答

1

你不應該處理的取消按鈕按。

由於按UIActionSheet上的所有按鈕均由actionSheet:clickedButtonAtIndex:處理,因此您需要檢查按鈕索引是否爲取消按鈕的索引。

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

} 
+0

感謝@rckoenes,這是工作的罰款:你可以用cancelButtonIndexUIActionSheet做到這一點 – user6183984

0

你可以試試這個

- (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 


}