iOS7_ios7_如何实现UIAlertView以及监听点击事件(其它样式)_如何修改UITextField默认键盘样式
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了iOS7_ios7_如何实现UIAlertView以及监听点击事件(其它样式)_如何修改UITextField默认键盘样式,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3030字,纯文字阅读大概需要5分钟。
内容图文
![iOS7_ios7_如何实现UIAlertView以及监听点击事件(其它样式)_如何修改UITextField默认键盘样式](/upload/InfoBanner/zyjiaocheng/1217/b0a0d2fc25c8470dbb7f05b100628d05.jpg)
首先我们知道,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> 45@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]; 1011// 拿到UITextField12 UITextField *tf = [altView textFieldAtIndex:0]; 1314// 设置textfield 的键盘类型。15 tf.keyboardType = UIKeyboardTypeNumberPad; 1617// 显示 alertView18 [altView show]; 1920 }
3、实现-(NSString*) altTitle 、-(NSString*) altMSG、-(NSString*) altTitle、-(NSString*) cancelBtnTitle、-(NSString*) otherBtnTitle方法。
1 // 返回 标题 2 - (NSString *)altTitle{ 3return@"下线通知"; 4} 5 6//返回 消息体 7 - (NSString *)altMSG{ 8return@"你的帐号在异地登录,密码可能泄露,建议前往http://mobodemy.com进行修改。"; 9} 1011//返回 退出按钮 标题12 - (NSString *) cancelBtnTitle { 13return@"退出"; 14} 1516//返回 重新登录 按钮标题17 - (NSString *) otherBtnTitle { 18return@"重新登录"; 19 }
4、实现监听按钮点击方法
1 // 监听点击时间 代理方法 2 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 3{ 4 NSString *btnTitle = [alertView buttonTitleAtIndex:buttonIndex]; 5if ([btnTitle isEqualToString:[self cancelBtnTitle]]) { 6 NSLog(@"你点击了退出"); 7 } 8elseif ([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]; 23 [alertView show];
2、设定 alertView 的类型:
[altView setAlertViewStyle:UIAlertViewStyleSecureTextInput];
3、拿到 textField的 键盘,并设置类型:
1 // 拿到UITextField 2 UITextField *tf = [alertView textFieldAtIndex:0]; 34// 设置textfield 的键盘类型。5 tf.keyboardType = UIKeyboardTypeNumberPad; //设定为 数字键盘
4、别忘了遵守代理协议,才能实现代理方法,否则会报错。
1 // 监听点击时间 代理方法 2 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 3{ 4 NSString *btnTitle = [alertView buttonTitleAtIndex:buttonIndex]; 5if ([btnTitle isEqualToString:[self cancelBtnTitle]]) { 6 NSLog(@"你点击了退出"); 7 } 8elseif ([btnTitle isEqualToString:[self otherBtnTitle]] ) { 9 NSLog(@"你点击了重新登录按钮"); 10 } 11 }
原文:http://www.cnblogs.com/LeoYoung/p/LeoYoung_QQ_907596253_TEL_15576003890.html
内容总结
以上是互联网集市为您收集整理的iOS7_ios7_如何实现UIAlertView以及监听点击事件(其它样式)_如何修改UITextField默认键盘样式全部内容,希望文章能够帮你解决iOS7_ios7_如何实现UIAlertView以及监听点击事件(其它样式)_如何修改UITextField默认键盘样式所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。