UIAlertView 可用來顯示提示訊息與按鈕。建立時可指定標題、內容、代理與按鈕文字。
UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"b"
message:@"s"
delegate:self
cancelButtonTitle:@"確定"
otherButtonTitles:@"取消", nil];
[a show];
按下按鈕後,會呼叫 UIAlertViewDelegate 的 clickedButtonAtIndex 方法。buttonIndex 用來判斷使用者按下哪個按鈕。
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@"%i", buttonIndex);
}
若要在不同按鈕執行不同行為,可根據 buttonIndex 分支處理。新建的 iOS 專案也可改用 UIAlertController,但舊有程式仍常看到這種寫法。