标签:ios8 uiactionsheet uialertview uialertcontroller
在iOS8之前用UIActionSheet和UIAlertView来提供按钮选择和提示性信息,比如UIActionSheet可以这样写:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"title,nil时不显示" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"第一项", @"第二项",nil]; actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque; [actionSheet showInView:self.view];
(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == 0) { NSLog(@"确定"); }else if (buttonIndex == 1) { NSLog(@"第一项"); }else if(buttonIndex == 2) { NSLog(@"第二项"); }else if(buttonIndex == actionSheet.cancleButtonIndex) { NSLog(@"取消"); } } - (void)actionSheetCancel:(UIActionSheet *)actionSheet{ } -(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{ } -(void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex{ }
- (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:@"确定"]) { [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; } else { [button setTitleColor:[WTDevice getGreenColor] forState:UIControlStateNormal]; } button.titleLabel.font = [UIFont systemFontOfSize:18]; } } }
#ifdef __IPHONE_8_0 if (TARGET_IS_IOS8) { UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:@"提示" message:@"需要设置允许访问相机,操作方法见“设置”->“帮助中心”" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"确定" 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:@"提示" message:@"需要设置允许访问相机,操作方法见“设置”->“帮助中心”" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil]; [alert show]; }
#ifdef __IPHONE_8_0 if (TARGET_IS_IOS8) { UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:@"action选项" message:nil preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction *action0 = [UIAlertAction actionWithTitle:@"选项一" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { [self customMethod1]; }]; [actionSheetController addAction:action0]; UIAlertAction *action = [UIAlertAction actionWithTitle:@"选项二" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { [self <span style="font-family: Arial, Helvetica, sans-serif;">customMethod2</span>]; }]; UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"选项三" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { [self customMethod3]; }]; UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel 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选项" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"选项一",@"选项二",@"选项三", nil]; [as showInView:self.view]; }
标签:ios8 uiactionsheet uialertview uialertcontroller
原文地址:http://blog.csdn.net/zwf_apple/article/details/41949379