New format of UIAlertView and UIActionSheet after iOS8.0, ios8.0uialertview
After iOS8.0, Apple updated the creation methods of UIAlertView and UIActionSheet. In previous versions, the two alert controls have their own creation methods, but after iOS8.0, they use the UIAlertController controller class for unified creation. However, the previous creation method is not effective in Versions later than iOS8.0 and can still be used normally. Next we will record the new writing method.
First, let's look at the example of Apple API writing:
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert" message:@"This is an alert." preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}]; [self presentViewController:alert animated:YES completion:nil];
In this example, an alert is created in its class method:
alertControllerWithTitle:message:preferredStyle:
PreferredStyle defines the type of the created prompt box, which is an enumerated value. You can select different prompt box styles.
The second class in the example: UIAlertAction creates an action: the user clicks a different button to trigger different events. However, in the example, this action is not added to the prompt box. We can use:
- (void)addAction:(UIAlertAction *)action;
This method adds the behavior to the prompt box.
Below is a small demo:
/*! @ Brief after iOS8.0, the creation methods of alert and action sheet have changed: they are uniformly created by UIAlertController. The previously created alert method is still available in ios8. @ since * // create an alert UIAlertController * alert = [UIAlertController alertControllerWithTitle: @ "Attention! "Message: @" Your Device Haven't PhotoLibrary. "preferredStyle: Regular]; // define action event UIAlertAction * action = [UIAlertAction actionWithTitle: @" OK "style: UIAlertActionStyleCancel handler: ^ (UIAlertAction * action) {NSLog (@ "callback after the OK button is clicked") ;}]; // Add the action time to the prompt box [alert addAction: action]; // The prompt box [self presentViewController: alert animated: YES completion: nil];