普通弹窗
1 #import "AppDelegate.h" 2 3 @interface AppDelegate () 4 @end 5 @implementation AppDelegate 6 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 7 8 self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]]; 9 self.window.backgroundColor = [UIColor whiteColor]; 10 [self.window makeKeyAndVisible]; 11 12 // 新建提示框 (小诀窍:用换行符来撑大UIAlert) 13 UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"提示框" message:@"hello word! " delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil]; 14 // 设置Alert的样式(一个文本输入框,一个密码框) 15 [alert setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput]; 16 // typedef enum{ 17 // UIAlertViewStyleDefault, // 默认样式 18 // UIAlertViewStyleLoginAndPasswordInput, // 有登陆效果的提示框(一个文本输入框,一个密码输入框) 19 // UIAlertViewStylePlainTextInput, // 文本输入框 20 // UIAlertViewStyleSecureTextInput // 密码输入框 21 // }UIAlertViewStyle 22 23 // 创建UITextField来关联UIAlert的输入框并设置键盘响应的样式 24 UITextField* field = [alert textFieldAtIndex:0]; 25 UITextField* field2 = [alert textFieldAtIndex:1]; 26 // 设置键盘类型 27 field.keyboardType = UIKeyboardTypeNumberPad; 28 field2.keyboardType = UIKeyboardTypeNumbersAndPunctuation; 29 30 // 添加子视图 [UIAlert需都为nil] (加载控件) UIActivityIndicatorView 31 UIActivityIndicatorView* activeView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 32 activeView.center = CGPointMake(alert.bounds.size.width/2.0f, alert.bounds.size.height-40.f); 33 [activeView startAnimating]; 34 [alert addSubview:activeView]; 35 alert.backgroundColor = [UIColor redColor]; 36 // 展示提示框 37 [alert show]; 38 39 return YES; 40 }
UIButton弹窗
1 #import "AppDelegate.h" 2 3 @interface AppDelegate ()<UIAlertViewDelegate> // 签UIAlertViewDelegate协议 4 // 协议方法按住comm键,用鼠标点进去看 5 @end 6 @implementation AppDelegate 7 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 8 // Override point for customization after application launch 9 self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]]; 10 self.window.backgroundColor = [UIColor whiteColor]; 11 [self.window makeKeyAndVisible]; 12 13 // 设置按钮,并初始化位置及大小 14 UIButton* button = [[UIButton alloc]initWithFrame:CGRectMake(20, 20, 80, 30)]; 15 // 设置按钮背景颜色 16 button.backgroundColor = [UIColor blueColor]; 17 // 设置显示文本及状态 18 [button setTitle:@"AlertView" forState:UIControlStateNormal]; 19 // 添加按钮点击事件 20 [button addTarget:self action:@selector(showAlert:) forControlEvents:UIControlEventTouchUpInside]; 21 // 添加按钮倒窗口 22 [self.window addSubview:button]; 23 return YES; 24 } 25 // 响应按钮点击事件 26 // 提示框也可以自定义,使用UI View及各种空件的排列组合实现 27 // Title:标题;message:内容;delegate:代理;其他的是按钮,按钮可以多个也可以没有 28 - (void)showAlert:(id)sender{ 29 // 索引时,“确定”的下标为0,“取消”的下标为1,以此类推。但显示时,@”确定“排列在最后,其他的安顺序排列 30 UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"提示内容" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil]; 31 32 /* 如果设置按钮为空时,想让提示框消失可以使用延时器设置好间隔时间让提示框自动消失; 33 UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"提示内容" delegate:self cancelButtonTitle:nil otherButtonTitles:nil]; 34 // 延时器 , repeats:循环 监听dian:事件 35 [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(dian:) userInfo:aler repeats:NO]; */ 36 // 展示提示框 37 [alert show]; 38 } 39 /* - (void)dian:(NSTimer*)sender{ 40 NSLog(@"监视器"); 41 NSLog(@"%@",sender.userInfo); 42 // 取消提示框 sender.userInfo提示框对象 43 [sender.userInfo dismissWithClickedButtonIndex:0 animated:YES]; 44 } */ 45 46 // 使用UIAlertViewDelegate协议方法 47 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 48 // 点击按钮后,获取按钮的索引值 49 switch (buttonIndex) { 50 case 0:{ 51 // 点击按钮后可以执行操作,如跳转页面......等等 52 NSLog(@"您点击了确定按钮"); 53 break; 54 }case 1:{ 55 NSLog(@"您点击了取消按钮"); 56 } 57 default: 58 break; 59 } 60 } 61 // 实现取消按钮的监听 62 - (void)alertViewCancel:(UIAlertView *)alertView{ 63 NSLog(@"您点击的取消按钮!!!"); 64 }
代理弹窗
1 #import "AppDelegate.h" 2 3 @interface AppDelegate () <UIAlertViewDelegate> // 签UIAlertViewDelegate协议 4 // 协议方法按住comm键,用鼠标点进去看 5 @end 6 @implementation AppDelegate 7 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 8 // Override point for customization after application launch 9 self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]]; 10 self.window.backgroundColor = [UIColor whiteColor]; 11 [self.window makeKeyAndVisible]; 12 13 UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"提示框" message:@"hello word!" delegate:self cancelButtonTitle:[self noButtonTitle] otherButtonTitles:[self yesButtonTitle], nil]; 14 // 展示提示框 15 [alert show]; 16 17 return YES; 18 } 19 // yes按钮 20 - (NSString*)yesButtonTitle{ 21 return @"Yes"; 22 } 23 // no按钮 24 - (NSString*)noButtonTitle{ 25 return @"No"; 26 } 27 // 判断用户按下的是Yes还是No (代理方法) 28 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 29 NSString* buttonTitle = [alertView buttonTitleAtIndex:buttonIndex]; 30 if ([buttonTitle isEqualToString:[self yesButtonTitle]]) { 31 NSLog(@"User pressed the Yes button"); 32 }else if([buttonTitle isEqualToString:[self noButtonTitle]]){ 33 NSLog(@"User pressed the No button"); 34 } 35 }