IOS-UIAlertView和UIActionSheet
IOS中兩大控制項:UIAlertView和UIActionSheet
UIAlertView是在螢幕中央彈出一個訊息框,該訊息框可以用來做訊息提示,也可以讓使用者選擇不同選項。
UIActionSheet是在螢幕底端彈出一個訊息框,功能類似UIAlertView,不過兩者除了位置不一樣外,其外觀也有出入。為了能夠響應UIAlertView和UIActionSheet,需要設定其代理,而對應的代理需要實現對應的協議(UIAlertViewDelegate,UIActionSheetDelegate)。實現以下兩個函數
// Called when a button is clicked. The view will be automatically dismissed after this call returns- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;// Called when a button is clicked. The view will be automatically dismissed after this call returns- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;
範例程式碼
代理實現協議
@interface ViewController : UIViewController
主要功能代碼
-(IBAction)onTextFieldEnd:(id)sender{ [_textTime resignFirstResponder]; // 顯示對話方塊#if 0 UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"TextContend" message:_textTime.text delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Good", @"Thanks", nil]; [alert show]; [alert release];#else // 底部彈出對話方塊 UIActionSheet* sheet = [[UIActionSheet alloc] initWithTitle:@"ActionSheet" delegate:self cancelButtonTitle:@"cancelButtonTitle" destructiveButtonTitle:@"destructiveButtonTitle" otherButtonTitles:@"OtherButton1", @"OtherButton2", nil]; [sheet showInView:self.view]; [sheet release];#endif}// 按鈕按下時響應函數- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ NSString* strInfo = nil; strInfo = [[NSString alloc] initWithFormat:@"你按下了【%@】按鈕,第%d個按鈕", [alertView buttonTitleAtIndex:buttonIndex], buttonIndex]; UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"按鈕響應代理函數" message:strInfo delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil]; [alert show]; [alert release]; [strInfo release];}// 按鈕按下時響應函數- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ NSString* strInfo = nil; strInfo = [[NSString alloc] initWithFormat:@"你按下了【%@】按鈕,第%d個按鈕", [actionSheet buttonTitleAtIndex:buttonIndex], buttonIndex]; UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"按鈕響應代理函數" message:strInfo delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil]; [alert show]; [alert release]; [strInfo release];}