UIActionSheet取消按钮无法正常工作

时间:2016-05-18 09:26:58

标签: ios objective-c xcode7 uiactionsheet

UIActionSheet,它有标题的按钮,我从数组中获取标题。我想获取按钮标题并显示在UILabel,我做了,但如果我按下取消按钮取消按钮也显示,我不想在UILabel中显示取消按钮标题  以下代码,我试过,

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

 }

请帮我这样做,

2 个答案:

答案 0 :(得分:2)

您不应按下取消按钮。

由于UIActionSheet上的所有按钮都由actionSheet:clickedButtonAtIndex:处理,因此您需要检查按钮索引是否为取消按钮的索引。您可以使用cancelButtonIndex上的UIActionSheet

执行此操作
- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if  (actionSheet.cancelButtonIndex == buttonIndex) {
        return;
    }

}

答案 1 :(得分: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


}