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     // 拿到UITextField
12     UITextField *tf = [altView textFieldAtIndex:0];
13     
14     // 设置textfield 的键盘类型。
15     tf.keyboardType = UIKeyboardTypeNumberPad;
16     
17     // 显示 alertView
18     [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 // 拿到UITextField
2      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 }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。