简体   繁体   English

UIActionSheet取消按钮无法正常工作

[英]UIActionSheet Cancel button is not working properly

UIActionSheet , it has buttons with title ,I am fetching the title from the array .I want to get the buttons title and displaying in UILabel , that i did , but if I press cancel button the cancel buttons also displaying ,I dont want to display the cancel button title in UILabel the below code which i have tried , 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];

 }

Please help me to do this , 请帮助我做到这一点,

You should not handle the cancel button press. 您不应处理取消按钮的按下。

Since all button presses on UIActionSheet are handled by actionSheet:clickedButtonAtIndex: you will need to check if the button index is that of the cancel button. 由于UIActionSheet上的所有按钮按下都是由actionSheet:clickedButtonAtIndex:处理的, actionSheet:clickedButtonAtIndex:您需要检查按钮索引是否为取消按钮的索引。 You can do this with the cancelButtonIndex on UIActionSheet : 您可以使用UIActionSheet上的cancelButtonIndex来做到这一点:

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

}

You can try this 你可以试试这个

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


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM