UIAlertController of iOS8 developed by iOS8
Before iOS8, use UIActionSheet and UIAlertView to provide button selection and prompt information. For example, you can write UIActionSheet as follows:
UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle: @ "title," delegate: self cancelButtonTitle: @ "cancel" destructiveButtonTitle: @ "OK" otherButtonTitles: @ "first", @ "second", nil]; actionSheet. actionSheetStyle = UIActionSheetStyleBlackOpaque; [actionSheet showInView: self. view];
Then implement the modern theory in the Protocol:
(Void) actionSheet :( UIActionSheet *) actionSheet clickedButtonAtIndex :( NSInteger) buttonIndex {if (buttonIndex = 0) {NSLog (@ "OK");} else if (buttonIndex = 1) {NSLog (@ "first");} else if (buttonIndex = 2) {NSLog (@ "second");} else if (buttonIndex = actionSheet. cancleButtonIndex) {NSLog (@ "cancel") ;}}-(void) actionSheetCancel :( UIActionSheet *) actionSheet {}-(void) actionSheet :( UIActionSheet *) actionSheet cancel :( NSInteger) buttonIndex {}-(void) actionSheet :( UIActionSheet *) actionSheet willDismissWithButtonIndex :( NSInteger) buttonIndex {}
To modify the font and color of a button, you can implement the following Proxy:
-(Void) willPresentActionSheet :( UIActionSheet *) actionSheet {for (UIView * subViwe in actionSheet. subviews) {if ([subViwe isKindOfClass: [UILabel class]) {UILabel * label = (UILabel *) subViwe; label. font = [UIFont systemFontOfSize: 16]; label. frame = CGRectMake (CGRectGetMinX (label. frame), CGRectGetMinY (label. frame), CGRectGetWidth (label. frame), CGRectGetHeight (label. frame) + 20);} if ([subViwe isKindOfClass: [UIButton class]) {UIButton * button = (UIButton *) subViwe; if ([button. titleLabel. text isEqualToString: @ "OK"]) {[button setTitleColor: [UIColor redColor] forState: UIControlStateNormal];} else {[button setTitleColor: [WTDevice getGreenColor] forState: UIControlStateNormal];} button. titleLabel. font = [UIFont systemFontOfSize: 18] ;}}}
The above code (proxy part) is valid in ios7 and later versions, but does not work in ios8. ios8. iOS8 discards UIActionSheet and UIAlertView and replaces it with UIAlertController, the usage is as follows (instead of UIAlertView ):
# Ifdef _ IPHONE_8_0 if (TARGET_IS_IOS8) {UIAlertController * actionSheetController = [UIAlertController alertControllerWithTitle: @ "prompt" message: @ "you need to set the permission to access the camera, for operation methods, see "Settings"> "Help Center" "preferredStyle: Custom]; UIAlertAction * actionCancel = [UIAlertAction actionWithTitle: @" OK "style: UIAlertActionStyleDestructive handler: ^ (UIAlertAction * action) {}]; [actionSheetController addAction: actionCancel]; [actionSheetController. view setTintColor: [WTDevice getGreenColor]; [self presentViewController: actionSheetController animated: YES completion: nil];} # endif if (TARGET_NOT_IOS8) {UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "prompt" message: @ "must be set to allow access to the camera, for the operation method, see "Settings"> "Help Center" "delegate: self cancelButtonTitle: @" cancel "otherButtonTitles: nil]; [alert show];}
Replace UIActionSheet:
# Ifdef _ DEFINE if (response) {UIAlertController * actionSheetController = [UIAlertController response: @ "action option" message: nil preferredStyle: Response]; UIAlertAction * action0 = [UIAlertAction actionWithTitle: @ "option 1" style: Custom handler: ^ (UIAlertAction * action) {[self customMethod1] ;}]; [actionSheetController addAction: action0]; UIAlertAction * action = [UIAlertAction actionWithTitle: @ "option 2" style: UIAlertActionStyleDefault handler: ^ (UIAlertAction * action) {[self customMethod2] ;}]; UIAlertAction * action1 = [UIAlertAction actionWithTitle: @ "option 3" style: UIAlertActionStyleDefault handler: ^ (UIAlertAction * action) {[self customMethod3] ;}]; UIAlertAction * actionCancel = [UIAlertAction actionWithTitle: @ "cancel" style: handler: ^ (UIAlertAction * action) {}]; [actionSheetController addAction: action]; [actionSheetController addAction: action1]; [actionSheetController addAction: actionCancel]; [actionSheetController. view setTintColor: [UIColor greenColor]; [self presentViewController: actionSheetController animated: YES completion: nil] ;}# endif if (TARGET_NOT_IOS8) {UIActionSheet * as = [[UIActionSheet alloc] initWithTitle: @ "action option" delegate: self cancelButtonTitle: @ "cancel" structivebuttontitle: nil otherButtonTitles: @ "option 1 ", @ "option 2", @ "option 3", nil]; [as showInView: self. view];}
As for the differences between the two, we can see that iOS8 previously overwrites a layer of view on the controller's view. After iOS8, a controller is displayed and the proxy is replaced with a block, the code is more compact.