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] 取消

運行的效果圖

相關文章
相關標籤/搜索