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

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

 UIAlertViewClass 的使用场景

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

   2,让用户确认一些动做this

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

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

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

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

       

 代码以下:code

#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

运行结果示意图  blog

       

 

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

    要得到用户点击的是那个按钮能够在 代码实现 UIAlertViewDelegate 代理中下面的方法索引

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

// 用于点击按钮是被调用,返回被调用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:0 ] ;
    user.keyboardType = UIKeyboardTypeAlphabet ;  //设置弹出的键盘样式
    
    UITextField *pass = [alterView textFieldAtIndex:1] ;
    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:0] ; UITextField *pass = [alertView textFieldAtIndex:1] ; 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] 取消

运行的效果图

相关文章
相关标签/搜索