UIActionSheet和UIAlertView在iOS中,都是弹出一个对话框,用户必须点击上面的按钮后才能执行其他操作。
下图是UIActionSheet,对话框显示在底部:
下图是AlertView,显示在屏幕正中:
当按钮在两个以内时,按钮是水平显示的,当超过两个时,会垂直显示。
相当代码:
首先在viewController h头文件添加UIActionSheetDelegate,UIAlertViewDelegate协议,如下:
1 2 |
#import <UIKit/UIKit.h> @interface TESTViewController : UIViewController <UIActionSheetDelegate,UIAlertViewDelegate> |
以下代码在ViewController.m文件中
打开ActionSheet对话框:
1 2 |
UIActionSheet *sheet=[[UIActionSheet alloc] initWithTitle:@"你确定?" delegate:self cancelButtonTitle:@"不确定" destructiveButtonTitle:@"非常确定" otherButtonTitles: nil]; [sheet showInView:self.view]; |
接收ActionSheet点击事件,以及打开AlertView对话框、接收AlertView点击事件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
-(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.alertViewStyle=UIAlertViewStyleDefault; //UIAlertViewStyleDefault 默认风格,无输入框 //UIAlertViewStyleSecureTextInput 带一个密码输入框 //UIAlertViewStylePlainTextInput 带一个文本输入框 //UIAlertViewLoginAndPasswordInput 带一个文本输入框,一个密码输入框 [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); //NSLog(@"输入 %@",[[alertView textFieldAtIndex:0] text]); 获取第一个文本框输入的文本,如果没有文件框,会异常,索引从0开始 }
|
相关推荐
NULL 博文链接:https://dingran.iteye.com/blog/1770376
iOS程序中的Action Sheet就像Windows中的 “确定-取消”对话框一样,用于强制用户进行选择。当用户将要进行的操作具有一定危险时,常常使用Action Sheet对用户进行危险提示,这样,用户有机会进行取消操作。 Alert...
ios应用源码之仿uialertview和uiactionsheet的一个效果
IOS应用源码之仿UIAlertView和UIActionSheet的一个效果 .rar
IOS应用源码——仿UIAlertView和UIActionSheet的一个效果.zip
以前在网上下了个时间选择器Demo,由于封装成UIActionSheet类型,而ios8弃用了UIActionSheet和UIAlertView,不得不对代码进行修改。将封装的UIActionSheet改为UIView,为了多态初始化时添加了枚举(几个时间选择器...
模仿IOS里面的UIActionSheet控件.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
IOS自定义UIActionSheet的例子,供大家学习
普通的列表对话框,使用简单,功能强大,可以自定义和强大,就像IOS中的UIActionSheet ##用 // Initialize an array of characters List< String> list = new ArrayList<> ( 3 ); list . add( " Send message ...
iOS系统自带的UIActionSheet不方便修改内部button的字体大小,颜色,本人模仿实现了个小demo可以完成上述功能85
简单的自定义UIActionSheet
BlockAlertsAnd-ActionSheets, 出色的UIAlertView和UIActionSheet替换灵感由 TweetBot 它是如何启动的: A的故事。自从我第一次在应用中使用UIActionSheet或者UIAlertView时,我不喜欢它的实现方式。 如果在同一类中...
NULL 博文链接:https://eric-gao.iteye.com/blog/2086946
仿UIAlertView和UIActionSheet的一个效果功能ios源码.rar
前两年IOS应用源码-主要是用于毕业设计学习的。
封装了UIAlertView和UIActionSheet的MessageBox
整合了UIAlertView和UIActionSheet这两种系统风格的弹框,创建弹框再也不用兼容iOS8以前和iOS8以后了。代码也不会因为delegate而分离,直接接受Block回调。如果您觉得我的代码帮到了您,我会非常欣慰。同时如果您想...
详细讲解UIAlertView和UIActionSheet的使用方法,以及开发中遇到的一些问题
IOS应用源码——UIActionSheet 的美化效果.rar