UIAlertView 是歷史上最基礎的「打斷式提示」方案:彈出、告知、等用戶點擊。它API極簡,但可玩性並不低——多按鈕、手動關閉、往裡塞加載指示器甚至輸入框,都能做到。本文按難度遞進地梳理一遍。
核心初始化方法:
- (instancetype)initWithTitle:(NSString *)title
message:(NSString *)message
delegate:(id /*<UIAlertViewDelegate>*/)delegate
cancelButtonTitle:(NSString *)cancelButtonTitle
otherButtonTitles:(NSString *)otherButtonTitles, ...;
調用方式:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"我的警告框"
message:@"這是一個警告框"
delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:@"確定", nil];
[alert show];

注意 otherButtonTitles 是可變參數,必須以 nil 結尾。如果按鈕超過兩個,系統會自動改成上下排列的樣式:

按鈕數量多到超出屏幕可視範圍時,列表會變成類似 tableView 的可滾動形態。
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
message:@"請選擇一個按鈕:"
delegate:nil
cancelButtonTitle:@"取消"
otherButtonTitles:@"按鈕一", @"按鈕二", @"按鈕三", nil];
[alert show];
[alert release];
UIAlertView 通過 UIAlertViewDelegate 把點擊事件回傳給你。頭文件中聲明:
@interface MyAlertViewViewController : UIViewController @end - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex; - (IBAction)buttonPressed;
實現文件:
- (IBAction)buttonPressed
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
message:@"請選擇一個按鈕:"
delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:@"按鈕一", @"按鈕二", @"按鈕三", nil];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *msg = [[NSString alloc] initWithFormat:@"您按下的是第 %d 個按鈕!", buttonIndex];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
message:msg
delegate:nil
cancelButtonTitle:@"確定"
otherButtonTitles:nil];
[alert show];
[alert release];
[msg release];
}
索引規則需要記牢:cancelButtonTitle 永遠是 0,其後依次是「取消」、「按鈕一」、「按鈕二」、「按鈕三」,對應 0、1、2、3。
[alert dismissWithClickedButtonIndex:0 animated:YES];
這裡有個容易踩的坑:如果刪掉全部按鈕,佈局並不會自動收緊。按鈕佔據的空間依然存在,只是變得不可見,整個結構會顯得頭重腳輕。若弹窗只用來展示文本,可以在 message 開頭加一個換行符(@"\n")來平衡上下留白。
下面示範添加一個菊花加載指示器:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"請等待"
message:nil
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:nil];
[alert show];
UIActivityIndicatorView *activeView =
[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activeView.center = CGPointMake(alert.bounds.size.width / 2.0f, alert.bounds.size.height - 40.0f);
[activeView startAnimating];
[alert addSubview:activeView];
[activeView release];
[alert release];
UIAlertView 的文本默認居中。想改成左對齊,或者塞進輸入框,可以借 willPresentAlertView: 這個「即將彈出」的時機來動手腳。
先來把消息文本改為左對齊:
- (void)willPresentAlertView:(UIAlertView *)alertView
{
for (UIView *view in alertView.subviews)
{
if ([view isKindOfClass:[UILabel class]])
{
UILabel *label = (UILabel *)view;
label.textAlignment = NSTextAlignmentLeft;
}
}
}
思路很樸素:彈出前遍歷所有子視圖,找到 UILabel 就把對齊屬性改掉。添加其他控件同理,下面是一個「賬號 + 密碼」登錄框的示範:
- (void)willPresentAlertView:(UIAlertView *)alertView
{
CGRect frame = alertView.frame;
frame.origin.y -= 120; // 整體上移,避免超出屏幕
frame.size.height += 80; // 撐高,騰出輸入框位置
alertView.frame = frame;
// 把原有的按鈕等子視圖下移,否則會被輸入框遮擋
for (UIView *view in alertView.subviews)
{
if (![view isKindOfClass:[UILabel class]])
{
CGRect btnFrame = view.frame;
btnFrame.origin.y += 70;
view.frame = btnFrame;
}
}
UITextField *accountName = [[UITextField alloc] init];
UITextField *accountPassword = [[UITextField alloc] init];
accountName.frame = CGRectMake(10, frame.origin.y + 40, frame.size.width - 20, 30);
accountPassword.frame = CGRectMake(10, frame.origin.y + 80, frame.size.width - 20, 30);
accountName.placeholder = @"請輸入賬號";
accountPassword.placeholder = @"請輸入密碼";
accountPassword.secureTextEntry = YES;
[alertView addSubview:accountPassword];
[alertView addSubview:accountName];
[accountName release];
[accountPassword release];
}
關鍵順序是:先調整弹窗自身 frame → 再把原有子視圖挪開 → 最後添加新控件。顛倒順序很容易出現遮擋。
底部動作菜單的定制思路完全一致,只要在對應的 willPresentActionSheet: 裡做同樣的遍歷與佈局調整即可:
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet;
UIAlertView 與 UIActionSheet 在 iOS 8 起已被官方標記為廢棄(deprecated),蘋果建議改用 UIAlertController。上面這些遍歷 subviews、直接改 frame 的技巧,本質上是依賴私有視圖層級的「黑科技」,在系統改版時隨時可能失效。新項目請優先使用 UIAlertController;閱讀本文更合適的定位是:理解舊代碼,以及在維護存量項目時知道坑在哪裡。