一.UIActionSheet(動作表) 和UIAlertView(警告)
UIActionSheet用於迫使使用者在兩個或更多的選項之間進行選擇的模式視圖。動作表從螢幕底部彈出,顯示一系列按鈕供使用者選擇,使用者只有單擊一個按鈕後才能繼續使用應用程式。
UIAlertView警告以藍色圓角矩形形式出現在螢幕中部,警報可顯示一個或多個按鈕
為了讓控制器類充當動作表的委託,控制器需要遵從UIActionSheetDelegate協議
二.UIActionSheet(動作表)的建立
initWithTitle:delegate:cancelButtonTitle:destructiveButtonTitle:otherButtonTitles:
注釋:initWithTitle是對象函數
initWithTitle:動作表標題
delegate:接收對象
cancelButtonTitle關閉按鈕標題
destructiveButtonTitle: 操作按鈕標題
otherButtonTitles:其他按鈕
1. 建立一個簡單的動作表:
//open a dialog with an OK and cancel button
UIActionSheet*actionSheet = [[UIActionSheet alloc]
initWithTitle:@" UIActionSheet<title>"
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:@"OK"
otherButtonTitles:nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheetshowInView:self.view]; //show from our table view (pops up in the middle of the table)
[actionSheetrelease];
2. 建立兩個按鈕的動作表
// open a dialog with an OKand cancel button
UIActionSheet*actionSheet = [[UIActionSheet alloc]
initWithTitle:@" UIActionSheet<title>"
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:@"OK"
otherButtonTitles:nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheetshowInView:self.view]; //show from our table view (pops up in the middle of the table)
[actionSheetrelease];
3. 建立自訂動作表:
//open a dialog with two custom buttons
UIActionSheet*actionSheet = [[UIActionSheet alloc]
initWithTitle:@" UIActionSheet<title>"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:@"Button1", @"Button2", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
actionSheet.destructiveButtonIndex = 1; // make the second button red(destructive)
[actionSheetshowInView:self.view]; //show from our table view (pops up in the middle of the table)
[actionSheet release];
對動作表中按鈕的操作用UIActionSheetDelegate協議函數,這裡你可以控制在點擊按鈕之後的操作
#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
//the user clicked one of the OK/Cancel buttons
if(buttonIndex == 0)
{
//NSLog(@"ok");
}
else
{
//NSLog(@"cancel");
}
}
三.UIAlertView(警告)的建立
initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:
注釋:initWithTitle是對象函數
initWithTitle:警告框標題
message:警告內容
delegate:接收對象
cancelButtonTitle:關閉按鈕標題
otherButtonTitles:其他按鈕
1. 建立一個簡單的警告框
//open an alert with just an OK button
UIAlertView*alert = [[UIAlertView alloc]
initWithTitle:@"UIAlertView"
message:@"<Alertmessage>"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];//顯示警告框
[alert release];//釋放警告框
2. 建立兩個按鈕的警告框
// open a alert with an OK andcancel button
UIAlertView*alert = [[UIAlertView alloc]
initWithTitle:@"UIAlertView"
message:@"<Alertmessage>"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", nil];
[alert show];//顯示警告框
[alert release];//釋放警告框
3. 建立自訂警告框
//open an alert with two custom buttons
UIAlertView*alert = [[UIAlertView alloc]
initWithTitle:@"UIAlertView"
message:@"<Alertmessage>"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Button1",@"Button2", nil];
[alert show];//顯示警告框
[alert release];//釋放警告框
4 警告框中按鈕的操作
對警告框中按鈕的操作用UIAlertViewDelegate協議函數,這裡你可以控制在點擊按鈕之後的操作
#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
//use "buttonIndex" to decide your action
//
}
5 給警告框添加子視圖:
UIAlertView*alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView " 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];
摘自 decemberd的專欄