iOS8新特性之UIAlertController – iPhone手機開發技術文章 iPhone軟體開發教學課程

iOS8把很多相識的控件結合在一起。

比如這節要講的:UIAlertController

UIAlertController 結合瞭UIAlert/UIActionSheet的所有功能

 

// 創建代碼
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@提示 message:@啊啊啊啊啊啊 preferredStyle:UIAlertControllerStyleAlert];

[self presentViewController:alert animated:YES completion:nil];


 

 

 

preferredStyle 是個枚舉

typedef NS_ENUM(NSInteger, UIAlertControllerStyle) { UIAlertControllerStyleActionSheet = 0, UIAlertControllerStyleAlert } NS_ENUM_AVAILABLE_IOS(8_0);

但是,這樣子彈出的框是沒有點擊按鈕的。

所以,必須添加按鈕事件

 


// UIAlertActionStyleCancel 是個類型枚舉 // typedef NS_ENUM(NSInteger, UIAlertActionStyle) { // UIAlertActionStyleDefault = 0, // UIAlertActionStyleCancel, // UIAlertActionStyleDestructive // } NS_ENUM_AVAILABLE_IOS(8_0);


UIAlertAction *action = [UIAlertAction actionWithTitle:@確定 style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        // 回調在block裡面
        // to do..
    }];
    [alert addAction:action];
    
    
    UIAlertAction *actionDelete = [UIAlertAction actionWithTitle:@刪除 style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        // to do..
    }];
    [alert addAction:actionDelete];

添加按鈕可以瞭,但是添加輸入框要怎麼辦呢?

 

類提供瞭一個方法:

– (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))configurationHandler;

 

    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @11;
    }];

可以在block裡面設置textField任意的屬性,更加靈活

@property (nonatomic, readonly) NSArray *textFields; // 獲取所有的輸入框

 

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *