下圖是UIActionSheet,對話框顯示在底部: spa
下圖是AlertView,顯示在屏幕正中:
code
首先在viewController h頭文件添加UIActionSheetDelegate,UIAlertViewDelegate協議,以下: orm
#import <UIKit/UIKit.h> @interface TESTViewController : UIViewController <UIActionSheetDelegate,UIAlertViewDelegate>接收ActionSheet點擊事件,以及打開AlertView對話框、接收AlertView點擊事件:
-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex { //該方法由UIActionSheetDelegate協議定義,在點擊ActionSheet的按鈕後自動執行 NSString *string=[NSString stringWithFormat:@"你點擊了 %@",[actionSheet buttonTitleAtIndex:buttonIndex]]; UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:string delegate:self cancelButtonTitle:@"肯定" otherButtonTitles:@"取消",nil]; [alert show]; } -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { //該方法由UIAlertViewDelegate協議定義,在點擊AlertView按鈕時自動執行,因此若是這裏再用alertView來彈出提//示,就會死循環,不停的彈AlertView NSString * string=[NSString stringWithFormat:@"你點擊了 %@",[alertView buttonTitleAtIndex:buttonIndex]]; // UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:string delegate:self cancelButtonTitle:@"肯定" otherButtonTitles:nil]; // [alert show]; NSLog(@"%@",string); }