Actionsheet和AlertView的使用
来源:互联网 发布:网络创业 编辑:程序博客网 时间:2024/05/06 21:01
AlertView是警告框,用于给用户以警告或者是提示,最多有两个按钮,超过两个按钮应该使用操作表Actionsheet。由于在IOS中,警告框是“模态”(不关闭它就不能做别的事情,Actionsheet也是如此,IOS是以MVC:模态--视图--控制为基理的)。接下来我将讲解今天所完成的例子:
1,从对象库中向故事版拖入两个button控件,分别命名为:test警告框,test操作表。然后将这两个控件设置为动作,.h文件中相关代码如下:
#import <UIKit/UIKit.h>
@interface ViewController :UIViewController<UIAlertViewDelegate,UIActionSheetDelegate>
- (IBAction)testAlertView:(id)sender;
- (IBAction)testActionSheet:(id)sender;
@end
2,完善testAlertView方法和testActionSheet方法:
2.1.1,testAlertView方法:
- (IBAction)testAlertView:(id)sender {
UIAlertView *alertView=[[UIAlertViewalloc] initWithTitle:@"Alert"message:@"It`s dangerous!Do you want to continue?" delegate:selfcancelButtonTitle:@"NO"otherButtonTitles:nil];
[alertView addButtonWithTitle:@"YES"]; //添加一个“YES”按钮
[alertViewshow];
}
备注:delegate参数在本例中设置为self,即警告框的委托对象时当前的视图控制器(ViewControll),cancelButtonTitle参数用于设置“取消”按钮的标题,它是警告框的左按钮,ohterButtonTitles参数是其他按钮,为一个字符串数组,以nil结尾。
效果图:
2.1.2在控制台显示所按的按钮,我们可以添加如下代码:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog (@"the alertView buttonIndex=%i",buttonIndex);
}
- (IBAction)testActionSheet:(id)sender {
UIActionSheet *actionsheet=[[UIActionSheetalloc]initWithTitle:nildelegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:@"破坏性按钮"otherButtonTitles:@"Facebook",@"新浪微博",nil];
//[actionsheet addButtonWithTitle:@"Wechat"];
actionsheet.actionSheetStyle=UIActionSheetStyleAutomatic;
[actionsheetshowInView:self.view];
}
备注:如果我们没有将 //[actionsheet addButtonWithTitle:@"Wechat"];注掉,那么显示效果如下,我们发现添加的按钮都在“取消”按钮之后,
2.2.2,同样,我们如果想在控制台显示点击的按钮,可以添加如下代码:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(@"the actionsheet buttonIndex=%i",buttonIndex);
}
效果图:另外,在网上搜到一篇比较详细介绍AlertView的帖子,可供我们参考:http://www.chengxuyuans.com/iPhone_IOS/61772.html。
- Actionsheet和AlertView的使用
- ActionSheet和AlertView的封装
- IOS8 alertView 和 actionSheet 使用方法
- AlertView&ActionSheet
- 操作表ActionSheet和警报AlertView
- IOS8 AlertView和ActionSheet不兼容问题解决方案
- iOS8.0前后关于alertView和actionSheet
- iOS AlertView、ActionSheet
- IOS中的通知--操作表ActionSheet和警报AlertView
- [IOS Code]实现全局关闭所有键盘,actionSheet和alertView
- IOS中的通知--操作表ActionSheet和警报AlertView
- iOS:AlerView、ActionSheet和AlertController的简单使用
- Iphone开发(六)IOS中的通知--操作表ActionSheet和警报AlertView
- Iphone开发(六)IOS中的通知--操作表ActionSheet和警报AlertView
- Iphone开发(六)IOS中的通知--操作表ActionSheet和警报AlertView
- Iphone开发(六)IOS中的通知--操作表ActionSheet和警报AlertView
- IOS SDK详解之UIAlertController(IOS8之后替代AlertView和ActionSheet)
- IOS SDK详解之UIAlertController(IOS8之后替代AlertView和ActionSheet)
- 不想丢失客户?看看这七招!
- 绍兴函授大专本科远程大专学历哪里报名好初中能不能报大专
- java设计模式---抽象工厂模式
- An Introduction to Reflection in C#
- 教你从零开始成为优秀交互设计师(四):工具资源
- Actionsheet和AlertView的使用
- 使用JQuery 表单动态添加和删除行
- Linux文件锁flock,检查程序是否已经运行
- Android UI布局之FrameLayout
- mySQL数据库Sql语句执行效率检查--Explain命令
- 虚函数与纯虚函数
- 类和接口
- 消灭Bug!18款最佳的问题跟踪管理应用程序
- 版本控制SVN与Eclipse4.4.1集成