ios/mac/COCOA系列 -- UIALertVIew 学习笔记

最近在学习ios开发,学习的书籍《ios7 Pragramming cookbook》,做笔记的目的以后方便查看。笔记形式是小例子,将书上的例子书写完整。

UIAlertViewClass 的使用场景

  1,向用户以一个警告的形式显示信息。

   2,让用户确认一些动作

   3,让用户输入用户名和密码

   4,让用户输入一些文本,这些文本在程序被使用

例1   实现简单显示一些警告信息

新建一个 Single View Application 简单工程,工程名字维AlterView,扩展前缀CB

ios/mac/COCOA系列 -- UIALertVIew 学习笔记 ios/mac/COCOA系列 -- UIALertVIew 学习笔记

代码如下:

#import "CBViewController.h"

@interface CBViewController ()
@end @implementation CBViewController - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIAlertView *alterView = [[UIAlertView alloc]
initWithTitle:@"学些MAC/IOS开发"
message:@"一个UIAlterView的例子"
delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:@"确定", nil];
[alterView show] ;
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

运行结果示意图

ios/mac/COCOA系列 -- UIALertVIew 学习笔记

例2  让用户输入账号和密码 ,log输出账号和密码

要获得用户点击的是那个按钮可以在 代码实现 UIAlertViewDelegate 代理中下面的方法

// Called when a button is clicked. The view will be automatically dismissed after this call returns

// 用于点击按钮是被调用,返回被调用button的索引

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;

#import "CBViewController.h"

@interface CBViewController ()<UIAlertViewDelegate> //添加代理

@end

@implementation CBViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. UIAlertView *alterView = [[UIAlertView alloc]
initWithTitle:@"学些MAC/IOS开发"
message:@"请输入你的账号和密码"
delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:@"确定", nil];
[alterView setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput] ; UITextField *user = [alterView textFieldAtIndex: ] ;
user.keyboardType = UIKeyboardTypeAlphabet ; //设置弹出的键盘样式 UITextField *pass = [alterView textFieldAtIndex:] ;
pass.keyboardType = UIKeyboardTypeDefault ; [alterView show] ;
}

////实现代理协议的方法
//// 用于点击按钮是被调用,返回被调用button的索引
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//通过返回button索引 得到点击按钮的现实字符串
NSString *buttonName = [alertView buttonTitleAtIndex:buttonIndex];
if ([buttonName isEqualToString:@"确定" ]) {
UITextField *user = [alertView textFieldAtIndex:] ;
UITextField *pass = [alertView textFieldAtIndex:] ;
NSLog(@"确定 账号=%@ 密码=%@", user.text, pass.text) ;
}
else if ([buttonName isEqualToString:@"取消"])
{
NSLog(@"取消");
}
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

输出结果

2014-07-09 15:27:05.980 AlertView[8406:60b] 确定账号=goipc 密码=123456

2014-07-09 15:39:31.938 AlertView[8429:60b] 取消

运行的效果图

ios/mac/COCOA系列 -- UIALertVIew 学习笔记

上一篇:使用BOM 的window对象属性打开新窗口


下一篇:centos 7安装phpMyadmin