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
中做同样的处理一样可以得到自己想要的界面。

来源:http://hi.baidu.com/richiechyi/item/e2aa2f714996f012d0dcb39a

UIAlertView用法相关推荐

  1. 关于iOS 7以后自定义UIAlertview(CustomIOS7AlertView)的用法心得

    公司项目要求更新的提示框上要加明更新的内容,如下图所示: 但是用系统自带的UIAlertview 却偏偏成为了这个样子! 不对称很难看有木有! 故从网上搜寻如何能使message的text左对齐 找到 ...

  2. 简述UIAlertView的属性和用法

    1.Title 获取或设置UIAlertView上的标题. 2.Message 获取或设置UIAlertView上的消息 UIAlertView *alertView = [[UIAlertView ...

  3. UICollectionViewController的用法1

    UICollectionView 和 UICollectionViewController 类是iOS6 新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableVie ...

  4. 【IOS 开发】基本 UI 控件详解 (UISegmentedControl | UIImageView | UIProgressView | UISlider | UIAlertView )

    转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/50163725 一. 分段控件 (UISegmentedControl) 控件展 ...

  5. ios开发入门篇(四):UIWebView结合UISearchBar的简单用法

     UIWebView是ios开发中比较常用的一个控件.我们可以用它来浏览网页.打开文档等,今天笔者在这里简单介绍下UIWebView和UISearchBar结合起来的用法,做一个简单的类浏览器. 一: ...

  6. c语言中external,static关键字用法

    static用法: 在C中,static主要定义全局静态变量.定义局部静态变量.定义静态函数. 1.定义全局静态变量:在全局变量前面加上关键字static,该全局变量变成了全局静态变量.全局静态变量有 ...

  7. Pandas_transform的用法

    先来看一个实例问题. 如下销售数据中展现了三笔订单,每笔订单买了多种商品,求每种商品销售额占该笔订单总金额的比例.例如第一条数据的最终结果为:235.83 / (235.83+232.32+107.9 ...

  8. Python中yield和yield from的用法

    yield 后面接的是 future 对象 调用方 委托生成器 yield from 直接给出循环后的结果 yield from 委托者和子生成器直接通信 yield from 直接处理stopIte ...

  9. pytorch学习 中 torch.squeeze() 和torch.unsqueeze()的用法

    squeeze的用法主要就是对数据的维度进行压缩或者解压. 先看torch.squeeze() 这个函数主要对数据的维度进行压缩,去掉维数为1的的维度,比如是一行或者一列这种,一个一行三列(1,3)的 ...

  10. python yield 和 yield from用法总结

    #例1. 简单输出斐波那契數列前 N 个数 #缺点:该函数可复用性较差,因为 fab 函数返回 None,其他函数无法获得该函数生成的数列 #要提高 fab 函数的可复用性,最好不要直接打印出数列,而 ...

最新文章

  1. Laravel教程 一:安装及环境配置
  2. 顺序表查找+折半查找(二级)
  3. 网络流--最大流--POJ 1273 Drainage Ditches
  4. 高级php平时的工作,【高级PHP开发工作内容|工作职责|高级PHP开发做什么】-看准网...
  5. Spring配置中的classpath:与classpath*:的区别研究
  6. android体系结构中每层的功能,Android体系结构
  7. 基本结构标签(HTML)
  8. 解决升级PHP7后 微信公众号收不到消息
  9. PC微信逆向:破解聊天记录文件!
  10. 光纤资料大全之光纤分类
  11. 【BZOJ】2286: [Sdoi2011消耗战【虚树DP】
  12. 【LeetCode - 379】电话目录管理系统
  13. 互联网公司测试组长/leader/经理如何面试社招测试工程师
  14. 计算机信息安全专业主要学习什么,信息安全专业学什么 课程有哪些
  15. EXCEL将网段地址分解成明细地址
  16. 简单好玩经典有趣的微信小游戏
  17. [刷题]leetcode\344_反转字符串
  18. ESP32 (Timer)-定时器学习(7)
  19. python全栈(一)网络通信与服务器之多任务-进程
  20. 中国人不便宜了,医疗信息化才有希望

热门文章

  1. jmeter-01 JMeter HTTP测试的各元件功能演示示例
  2. bzoj2705 [SDOI2012]Longge的问题
  3. 带reportView的winform程序在部署安装的时需要装两个框架,一framework框架二就是reportviewer的安装包...
  4. 每日一课(4/75)逻辑运算指令
  5. CUDA Fortran for Scientists and Engineers第二版翻译
  6. self-hacking第五天
  7. 推荐系统(recommender systems):预测电影评分--构造推荐系统的一种方法:协同过滤(collaborative filtering )...
  8. python plt 如何画不同的数据图
  9. 厚积薄发 - 关于runtime的几个问题
  10. 工欲善其事必先利其器——dreamweaver