下图是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); }