首页 > 代码库 > iOS7_ios7_如何实现UIAlertView以及监听点击事件(其它样式)_如何修改UITextField默认键盘样式
iOS7_ios7_如何实现UIAlertView以及监听点击事件(其它样式)_如何修改UITextField默认键盘样式
首先我们知道,UIAlertView实际上有多种样式,在xcode中,按住cmd点击UIAlertView,进入头文件我们看到:
1 typedef NS_ENUM(NSInteger, UIAlertViewStyle) {2 UIAlertViewStyleDefault = 0, //默认样式3 UIAlertViewStyleSecureTextInput, //加密文本样式4 UIAlertViewStylePlainTextInput, //普通文本样式5 UIAlertViewStyleLoginAndPasswordInput //帐号密码样式6 };
其次,我们实现一下加密文本样式的UIAlertView:
1、在 viewController.m 文件中的 @interface部分,遵守 <UIAlertViewDelegate> 协议:
1 #import "moboViewController.h"2 3 @interface moboViewController () <UIAlertViewDelegate>4 5 @end
2、在- (void) viewDidLoad 方法中初始化 UIAlertView的一个 实例,并且设定其样式为 加密文本样式。
1 - (void)viewDidLoad 2 { 3 [super viewDidLoad]; 4 5 // 初始化 alertView,设定代理为 self 6 UIAlertView *altView = [[UIAlertView alloc]initWithTitle:[self altTitle] message:[self altMSG] delegate:self cancelButtonTitle:[self cancelBtnTitle] otherButtonTitles:[self otherBtnTitle], nil]; 7 8 // 设定altView类型 9 [altView setAlertViewStyle:UIAlertViewStyleSecureTextInput];10 11 // 拿到UITextField12 UITextField *tf = [altView textFieldAtIndex:0];13 14 // 设置textfield 的键盘类型。15 tf.keyboardType = UIKeyboardTypeNumberPad;16 17 // 显示 alertView18 [altView show];19 20 }
3、实现-(NSString*) altTitle 、-(NSString*) altMSG、-(NSString*) altTitle、-(NSString*) cancelBtnTitle、-(NSString*) otherBtnTitle方法。
1 //返回 标题 2 - (NSString *)altTitle{ 3 return @"下线通知"; 4 } 5 6 //返回 消息体 7 - (NSString *)altMSG{ 8 return @"你的帐号在异地登录,密码可能泄露,建议前往http://mobodemy.com进行修改。"; 9 }10 11 //返回 退出按钮 标题12 - (NSString *) cancelBtnTitle {13 return @"退出";14 }15 16 //返回 重新登录 按钮标题17 - (NSString *) otherBtnTitle {18 return @"重新登录";19 }
4、实现监听按钮点击方法
1 //监听点击时间 代理方法 2 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 3 { 4 NSString *btnTitle = [alertView buttonTitleAtIndex:buttonIndex]; 5 if ([btnTitle isEqualToString:[self cancelBtnTitle]]) { 6 NSLog(@"你点击了退出"); 7 } 8 else if ([btnTitle isEqualToString:[self otherBtnTitle]] ) { 9 NSLog(@"你点击了重新登录按钮");10 }11 }
最后,总结一下,其它三种样式的AlertView基本上都是这样实现、修改键盘样式、监听按钮点击事件的,主要记住下面几个方法和技巧就好:
1、创建一个 UIAlertView的实例,并显示出来:
1 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:<#(NSString *)#> message:<#(NSString *)#> delegate:<#(id)#> cancelButtonTitle:<#(NSString *)#> otherButtonTitles:<#(NSString *), ...#>, nil];2 3 [alertView show];
2、设定 alertView 的类型:
[altView setAlertViewStyle:UIAlertViewStyleSecureTextInput];
3、拿到 textField的 键盘,并设置类型:
1 // 拿到UITextField2 UITextField *tf = [alertView textFieldAtIndex:0];3 4 // 设置textfield 的键盘类型。5 tf.keyboardType = UIKeyboardTypeNumberPad; //设定为 数字键盘
4、别忘了遵守代理协议,才能实现代理方法,否则会报错。
1 //监听点击时间 代理方法 2 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 3 { 4 NSString *btnTitle = [alertView buttonTitleAtIndex:buttonIndex]; 5 if ([btnTitle isEqualToString:[self cancelBtnTitle]]) { 6 NSLog(@"你点击了退出"); 7 } 8 else if ([btnTitle isEqualToString:[self otherBtnTitle]] ) { 9 NSLog(@"你点击了重新登录按钮");10 }11 }
iOS7_ios7_如何实现UIAlertView以及监听点击事件(其它样式)_如何修改UITextField默认键盘样式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。