顯示廣告
隱藏 ✕
看板 Knuckles_note
作者 Knuckles (站長 那克斯)
標題 [Xcode] 使用 UIAlertView 產生對話視窗
時間 2015年03月13日 Fri. AM 02:40:46


UIAlertView 的用法

只是要顯示訊息的話

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Test" message:@"測試訊息" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil];
[alertView show];

對話視窗會長這樣:
[圖]



要讓使用者選擇確定或取消的話

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Test" message:@"測試訊息" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定",nil];
[alertView show];

對話視窗會長這樣:
[圖]


只有兩個選項時,第一個按鈕 cancelButton 在左邊,第二個按鈕 otherButton 在右邊
在 iOS 的習慣是將確定鈕放在右邊

要使用 UIAlertView 的 delegate 函數來取得使用者是點了什麼
所以 delegate: 的輸入值要改成 self

前面的 @interface xxxViewController () 行尾要加上
<UIAlertViewDelegate>

然後加上 delegate 函數:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(buttonIndex==0){
        //點了第一個鈕(取消),要做的事
    }else if(buttonIndex==1){
        //點了第二個鈕(確定),要做的事
    }
}


有超過兩個選項的話

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Test" message:@"測試訊息" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"選項1",@"選項2",@"選項3",nil];
[alertView show];

對話視窗長這樣:
[圖]


第一個按鈕 cancelButton 在最下面,後面的 otherButton 依序從上面排下來

delegate 函數可以這樣寫:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    switch(buttonIndex){
    case 0:
        //點了取消,要做的事
        break;
    case 1:
        //點了選項1,要做的事
        break;
    case 2:
        //點了選項2,要做的事
        break;
    case 3:
        //點了選項3,要做的事
        break;
    }
}


若是一個類別裡有用到不同的 UIAlertView
結果都只會回傳給同一個 delegate 函數
要使用 tag 來區分

例如有用到兩個 UIAlertView

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Test1" message:@"測試訊息1" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定",nil];
alertView.tag = 1;
[alertView show];

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Test2" message:@"測試訊息2" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定",nil];
alertView.tag = 2;
[alertView show];

delegate 函數可以這樣寫:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(alertView.tag==1){ //是由第一個UIAlertView呼叫的
        if(buttonIndex==0){
            //點了第一個鈕(取消),要做的事
        }else if(buttonIndex==1){
            //點了第二個鈕(確定),要做的事
        }
    }else if(alertView.tag==2){ //是由第二個UIAlertView呼叫的
        if(buttonIndex==0){
            //點了第一個鈕(取消),要做的事
        }else if(buttonIndex==1){
            //點了第二個鈕(確定),要做的事
        }
    }
}


若是要讓使用者可以輸入字串的話

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"請輸入字串" message:@"測試輸入字串" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定",nil];
[alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
[alertView show];

對話視窗會長這樣:
[圖]


在 delegate 函數接收使用者輸入的字串
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(buttonIndex==0){
        //點了取消
    }else if(buttonIndex==1){
        //點了確定,在Log顯示使用者輸入的字串
        UITextField *textfield =  [alertView textFieldAtIndex: 0];
        NSLog(@"user input:%@",[textfield text]);
    }
}


--
※ 作者: Knuckles 時間: 2015-03-13 02:40:46
※ 編輯: Knuckles 時間: 2015-11-26 11:11:57
※ 看板: KnucklesNote 文章推薦值: 0 目前人氣: 0 累積人氣: 1023 
分享網址: 複製 已複製
r)回覆 e)編輯 d)刪除 M)收藏 ^x)轉錄 同主題: =)首篇 [)上篇 ])下篇