Block for UIAlertViewDelegate

前端 未结 11 1388
悲哀的现实
悲哀的现实 2020-12-23 12:15

I\'m pretty new to objective C and I\'m just trying to figure out if I can use a block or a selector as the UIAlertViewDelegate argument for UIAlertView - and which is more

11条回答
  •  生来不讨喜
    2020-12-23 12:58

    One must use UIAlertController for that approach as Apple document says

    A UIAlertController object displays an alert message to the user. This class replaces the UIActionSheet and UIAlertView classes for displaying alerts. After configuring the alert controller with the actions and style you want, present it using the presentViewController:animated:completion: method.

    In addition to displaying a message to a user, you can associate actions with your alert controller to give the user a way to respond. For each action you add using the addAction: method, the alert controller configures a button with the action details. When the user taps that action, the alert controller executes the block you provided when creating the action object. Apple Docs.

    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
    
    
           message:@"This is an alert."
                                       preferredStyle:UIAlertControllerStyleAlert];
    
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
           handler:^(UIAlertAction * action) {}];
    
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
    

提交回复
热议问题