iOS开发---“弹框”
1、一、UIAlertView (但在ios9以后已经不在提倡此方法)
随着苹果上次iOS 5的发布,对话框视图样式出现在了我们面前,直到现在它都没有发生过很大的变化。下面的代码片段展示了如何初始化和显示一个带有“取消”和“好的”按钮的对话框视图。
//提示样式(4种)
@property(nonatomic,assign) UIAlertViewStyle alertViewStyle
// UIAlertViewStyleDefault ---默认样式
// UIAlertViewStyleSecureTextInput ---带有Password输入框
// UIAlertViewStylePlainTextInput ---输入框
// UIAlertViewStyleLoginAndPasswordInput --Login和Password的输入框
1.只有“确定” “取消” 两个按钮 默认样式为UIAlertViewStyleDefault
//排列 取消--0 确定--1
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示"message:@"确认退出登录?"delegate:self cancelButtonTitle:@"取消"otherButtonTitles:@"确定",nil];
alert.alertViewStyle=UIAlertViewStyleDefault; //显示样式
[alert show]; //提示框显示
//===点击后执行的方法===
#pragma mark-----UIAlertViewDelegate 使用UIAlertView时 遵循的协议--
//UIAlertViewDelegate 使用UIAlertView时 遵循的协议
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) { //取消
[alertView dismissWithClickedButtonIndex:0 animated:YES];
NSLog(@"点击了取消按钮");
}else if (buttonIndex == 1){ //确定
NSLog(@"点击了确定按钮");
}
}
2、2.有多个按钮的提示框
//@"取消" @"确定",@"AA",@"BB", 排列--0 1 2 3
//UIAlertViewDelegate 使用UIAlertView时 遵循的协议 有回调方法
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示"message:@"确认退出登录?"delegate:self cancelButtonTitle:@"取消"otherButtonTitles:@"确定",@"AA",@"BB",nil];
alert.alertViewStyle=UIAlertViewStyleDefault;
[alert show];
3、二、UIActionSheet
//提示样式(4种)
@property(nonatomic) UIActionSheetStyle actionSheetStyle;
//UIActionSheetStyleAutomatic --
//UIActionSheetStyleDefault --默认样式
//UIActionSheetStyleBlackTranslucent
//UIActionSheetStyleBlackOpaque
1.只有“确定” “取消” 两个按钮 默认样式为UIAlertViewStyleDefault
//排序 是--0 不是--1 取消--2
UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:@"我是最牛逼的开发"delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"是",@"不是",nil];
sheet.actionSheetStyle = UIActionSheetStyleDefault;
[sheet showInView:self.view];
//===点击后执行的方法===
#pragma mark-------UIActionSheetDelegate UIActionSheet 遵循的协议
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
// if (buttonIndex == 0) {
// NSLog(@"%@",actionSheet.title);
// }
NSLog(@"%ld",buttonIndex);
}
4、三、UIAlertController (我们最常用的 ios9以后推荐的使用方法)
//提示框 弹出的方向
typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
UIAlertControllerStyleActionSheet = 0, //底部弹出
UIAlertControllerStyleAlert //中间出现
} NS_ENUM_AVAILABLE_IOS(8_0);
//提示框样式
typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
UIAlertActionStyleDefault = 0, //默认
UIAlertActionStyleCancel, //显示在最下面 或 在左边
UIAlertActionStyleDestructive //变为红色
} NS_ENUM_AVAILABLE_IOS(8_0);
1.UIAlertController 单独使用
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示"message:@"我是最牛逼的开发者"preferredStyle:UIAlertControllerStyleActionSheet];
[alert addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self CamarPhotos]; //选择相机
[alert dismissViewControllerAnimated:YES completion:nil];
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self SelectPhotos]; // 选择相册
[alert dismissViewControllerAnimated:YES completion:nil];
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
[alert dismissViewControllerAnimated:YES completion:nil]; //返回之前的界面
}]];
[self presentViewController:alert animated:YES completion:nil]; //跳转到弹框
5、2. UIAlertController 和 UIAlertAction 一起使用
typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
UIAlertActionStyleDefault = 0, //默认
UIAlertActionStyleCancel, //在左边 不能同时设置2个)
UIAlertActionStyleDestructive //变为红色
} NS_ENUM_AVAILABLE_IOS(8_0);
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"你确定退出?" message:nil preferredStyle:UIAlertControllerStyleAlert];
//UIAlertActionStyleDefault
//UIAlertActionStyleCancel //在左边 (不能同时设置2个)
//UIAlertActionStyleDestructive //变为红色
//确定
UIAlertAction *okAlert = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
//具体操作内容
}];
//取消
UIAlertAction *cancelAlert = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action){
//具体操作内容
}];
[alert addAction:okAlert];
[alert addAction:cancelAlert];
[self presentViewController:alert animated:YES completion:nil];
6、3.UIAlertController 带有输入框的样式
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示"message:@"我是最牛逼的发"preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField*textField) {
textField.textColor= [UIColor redColor];
textField.text=@"iOS8";
//输入框文字改变时 方法
[textField addTarget:self action:@selector(usernameDidChange:)forControlEvents:UIControlEventEditingChanged];
}];
[alert addAction:[UIAlertAction actionWithTitle:@"确定"style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
NSLog(@"点击了确定按钮");
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"取消"style:UIAlertActionStyleCancel handler:^(UIAlertAction*action) {
NSLog(@"点击了取消按钮");
}]];
[self presentViewController:alert animated:YES completion:nil];
//输入框文字改变时 方法
-(void)usernameDidChange:(UITextField *)fd{
NSLog(@"%@",fd.text);
}
7、====UIActivityIndicatorView(菊花/指示器)====
代码如下图