UIActionSheet和UIAlert
UIActionSheet:
initWithTitle:@"Title"
delegate:self
cancelButtonTitle:@"Cancel!"
destructiveButtonTitle:@"OK!"
otherButtonTitles:nil];
[actionSheet showInView:self.view];//参数指显示UIActionSheet的parent。
[actionSheet release];
initWithTitle:@"Alert Title"
message:@"Alter Message Content"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
[alertView release];
1. 最简单的用法
UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"
message:@"这是一个简单的警告框!"
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
[alert release];
2. 为UIAlertView添加多个按钮
UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"
message:@"请选择一个按钮:"
delegate:nil
cancelButtonTitle:@"取消"
otherButtonTitles:@"按钮一", @"按钮二", @"按钮三",nil];
[alert show];
[alert release];
3. 如何判断用户点击的按钮
UIAlertView有一个委托UIAlertViewDelegate ,继承该委托来实现点击事件
头文件:
@interface MyAlertViewViewController : UIViewController<UIAlertViewDelegate> {
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
-(IBAction) buttonPressed;
@end
源文件:
-(IBAction) buttonPressed
{
UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"
message:@"请选择一个按钮:"
delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:@"按钮一", @"按钮二", @"按钮三",nil];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString* msg = [[NSString alloc] initWithFormat:@"您按下的第%d个按钮!",buttonIndex];
UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"提示"
message:msg
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
[alert release];
[msg release];
}
点击“取消”,“按钮一”,“按钮二”,“按钮三”的索引buttonIndex分别是0,1,2,3
4. 手动的取消对话框
[alertdismissWithClickedButtonIndex:0 animated:YES];
5:为UIAlertView添加子视图
在为UIAlertView对象太添加子视图的过程中,有点是需要注意的地方,如果删除按钮,也就是取消UIAlerView视图中所有的按钮的时候,可能会导致整个显示结构失衡。按钮占用的空间不会消失,我们也可以理解为这些按钮没有真正的删除,仅仅是他不可见了而已。如果在UIAlertview对象中仅仅用来显示文本,那么,可以在消息的开头添加换行符(@"\n)有助于平衡按钮底部和顶部的空间。
下面的代码用来演示如何为UIAlertview对象添加子视图的方法。
UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"请等待"
message:nil
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:nil];
[alert show];
UIActivityIndicatorView*activeView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activeView.center = CGPointMake(alert.bounds.size.width/2.0f, alert.bounds.size.height-40.0f);
[activeView startAnimating];
[alert addSubview:activeView];
[activeView release];
[alert release];
6. UIAlertView默认情况下所有的text是居中对齐的。 那如果需要将文本向左对齐或者添加其他控件比如输入框时该怎么办呢? 不用担心, iPhone SDK还是很灵活的, 有很多delegate消息供调用程序使用。 所要做的就是在
- (void)willPresentAlertView:(UIAlertView *)alertView
中按照自己的需要修改或添加即可, 比如需要将消息文本左对齐,下面的代码即可实现:
-(void) willPresentAlertView:(UIAlertView *)alertView
{
for( UIView * view in alertView.subviews )
{
if( [view isKindOfClass:[UILabel class]] )
{
UILabel* label = (UILabel*) view;
label.textAlignment=UITextAlignmentLeft;
}
}
}
这段代码很简单, 就是在消息框即将弹出时,遍历所有消息框对象,将其文本对齐属性修改为 UITextAlignmentLeft即可。
添加其他部件也如出一辙, 如下代码添加两个UITextField:
-(void) willPresentAlertView:(UIAlertView *)alertView
{
CGRect frame = alertView.frame;
frame.origin.y -= 120;
frame.size.height += 80;
alertView.frame = frame;
for( UIView * viewin alertView.subviews )
{
if( ![viewisKindOfClass:[UILabelclass]] )
{
CGRect btnFrame = view.frame;
btnFrame.origin.y += 70;
view.frame = btnFrame;
}
}
UITextField* accoutName = [[UITextFieldalloc] init];
UITextField* accoutPassword = [[UITextFieldalloc] init];
accoutName.frame = CGRectMake( 10, frame.origin.y + 40,frame.size.width - 20, 30 );
accoutPassword.frame = CGRectMake( 10, frame.origin.y + 80,frame.size.width -20, 30 );
accoutName.placeholder = @"请输入账号";
accoutPassword.placeholder = @"请输入密码";
accoutPassword.secureTextEntry = YES;
[alertView addSubview:accoutPassword];
[alertView addSubview:accoutName];
[accoutName release];
[accoutPassword release];
}
显示将消息框固有的button和label移位, 不然添加的text field会将其遮盖住。 然后添加需要的部件到相应的位置即可。
对于UIActionSheet其实也是一样的, 在
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
中做同样的处理一样可以得到自己想要的界面。
UIActionSheet和UIAlert的更多相关文章
- iOS开发系列--视图切换
概述 在iOS开发中视图的切换是很频繁的,独立的视图应用在实际开发过程中并不常见,除非你的应用足够简单.在iOS开发中常用的视图切换有三种,今天我们将一一介绍: UITabBarController ...
- 十、ios 模态窗口[实例]
一.模态窗口概念 对话框一般分为两种类型:模态类型( modal )与非模态类型( modeless ).所谓模态对话框,就是指除非采取有效的关闭手段,用户的鼠标焦点或者输入光标将一直停留在其上的对话 ...
- iphone内容开发技术学习
一.iOS基础 1 开发环境搭建以及IOS组件.框架的概要介绍. 2 mac操作系统与iOS操作系统 3 xcode IDE开发环境的初始 二.C语言基础 1数据类型.表达式与控制流程语句 2数组.函 ...
- iphone开发技术要学习的内容
一.iOS基础 1 开发环境搭建以及IOS组件.框架的概要介绍. 2 mac操作系统与iOS操作系统 3 xcode IDE开发环境的初始 二.C语言基础 1数据类型.表达式与控制流程语句 2数组.函 ...
- 【iOS开发-56】案例BUG:button的enabled、控件的userInteractionEnabled以及两种提示框UIAlert和UIActionSheet
接上述案例找BUG:[iOS开发-51]案例学习:动画新写法.删除子视图.视图顺序.延迟方法.button多功能使用方法及icon图标和启动页设置 (1)BUG:答案满了就不能再点击optionbut ...
- 弹框控件 UIAlertView UIActionSheet
// 创建弹框 从底部弹出,一般用于危险操作 UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"恭喜通关" ...
- UI第十三节——UIActionSheet
- (void)viewDidLoad { [super viewDidLoad]; UISwitch *swc = [[UISwitch alloc] initWithFrame ...
- 【代码笔记】iOS-清除图片缓存UIActionSheet
一,效果图. 二,代码. RootViewController.m //点击任何处出现sheet -(void)touchesBegan:(NSSet *)touches withEvent:(UIE ...
- UIAlertView 与 UIActionSheet (提示用户)的使用方法
UIAlertView 提示用户 帮助用户选择框 // UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"警 ...
随机推荐
- GCD Demo (先存起来自己看)
// 原代码块一 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // 原代码块二 N ...
- C++ version the delaunay triangulation
https://github.com/Bl4ckb0ne/delaunay-triangulation
- .NET Framework 4 和 .NET Framework 4 Client Profile
提出这个问题的背景:在新项目中封装了一个DAL来通过EF框架对数据库操作,但是在项目引用后,每当编译时就会出错!更加诡异的是在Web项目和WCF项目中对此DAL引用时就不会出现此问题.作为一个初学者, ...
- C# 调用VC++的DLL,VC++封装DLL
VS中新建一个动态库项目 文件生成一个工程名对应的.cpp文件,该文件定义 DLL应用程序的导出函数. 工程内新建一个类OutputInt,我用类向导生成,工程中会添加OutputInt.cpp和Ou ...
- python 之 append extend
概述 append和extend针对python的列表 列表内的元素为对象,可以为数字.字符串.列表等等 append添加的是一个对象 extend添加一个列表 例子 append >>& ...
- SQL Update实现使用一个表的数据更新另一张表
表结构 功能 SQL Serevr Access 表结构
- C# 字典 Dictionary<Tkey,Tvalue>
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来.我们都知道计算机技术发展日新月异,速度惊人的快,你我稍不留神,就会被慢慢淘汰!因此:每日不间断的学习是避免被 ...
- SqlServer跨库查询
由于业务的拆分,数据库拆分为两种作用: 汇总数据库(Master,头节点数据库), 子节点数据库(Compute Node,计算子节点数据库) 这样,就设计到子节点访问头节点数据库中的某张汇总表,这种 ...
- 20145207 《Java程序设计》第10周学习总结
前言: 最后一篇java博客好激动啊..不过猜猜我在干什么?没错,安虚拟机,唉!紧接着又是一大波信安系统的博客,真开心~好啦边敲博客,边装虚拟机. 教材知识汇总 13.1 网络概述 13.1.1计 ...
- SQL封装、多态与重载
面向对象1.类:众多对象抽象出来的2.对象:类实例化出来的 3.类的定义关键字 class 4.类里面包含成员变量成员属性 成员方法 5.面向对象三大特性(1)封装目的:保护类,让类更加安全.做法:让 ...