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. UIAlertController UIAlertView用法

    项目中很多地方会出现弹出框框,来做个判断 基本方法如下 UIAlertController *alertC = [UIAlertController alertControllerWithTitle: ...

  2. iOS开发之UIAlertView与UIAlertController的详尽用法说明

    本文将从四个方面对IOS开发中UIAlertView与UIAlertController的用法进行讲解: 一.UIAlertView与UIAlertController是什么东东? 二.我们为什么要用 ...

  3. swift - UIAlertView 的用法

    1,创建一个alertview,并带有“确定”和“取消”两个按钮 (注:在这里使用alertview,会报警告,那是因为从ios 8 以后,建议使用UIAlertviewController) //警 ...

  4. 【转】 UIALertView的基本用法与UIAlertViewDelegate对对话框的事件处理方法

    原文网址:http://blog.csdn.net/enuola/article/details/7900346 首先,视图控制器必须得实现协议UIAlertViewDelegate中的方法,并指定d ...

  5. UIALertView的基本用法与UIAlertViewDelegate对对话框的事件处理方法

    首先,视图控制器必须得实现协议UIAlertViewDelegate中的方法,并指定delegate为self,才能使弹出的Alert窗口响应点击事件. 具体代码如下: ViewController. ...

  6. (转) UIALertView的基本用法与UIAlertViewDelegate对对话框的事件处理方法

    首先,视图控制器必须得实现协议UIAlertViewDelegate中的方法,并指定delegate为self,才能使弹出的Alert窗口响应点击事件. 具体代码如下: #import <UIK ...

  7. UIALertView与UIAlertViewDelegate的基本用法

    首先,视图控制器必须得实现协议UIAlertViewDelegate中的方法,并指定delegate为self,才能使弹出的Alert窗口响应点击事件. 具体代码如下: ViewController. ...

  8. Swift - 告警框(UIAlertView)的用法

    1,下面代码创建并弹出一个告警框,并带有“取消”“确定”两个按钮 (注:自IOS8起,建议使用UIAlertController) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 ...

  9. 给iOS开发新手送点福利,简述UIAlertView的属性和用法

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

随机推荐

  1. 浅拷贝,深拷贝---ios

    #import <Foundation/Foundation.h> @interface Father : NSObject <NSCopying,NSMutableCopying& ...

  2. Twitter 登录和分享

    继上面一片介绍了FaceBook的登录和分享,现在再来实现Twitter的登录和分享. 1.首先要说明的是,我没找到官方提供的SDK,查阅很多文章都提到了一个帮助实现的包Twitter4j.jar   ...

  3. SaaS、PaaS和IaaS

    •SaaS(软件即服务) •PaaS(平台即服务) •IaaS(基础架构即服务)

  4. c#, 输出二进制

    int x=-17; string str= Convert.ToString(x,2);Debug.Log(str); 输出结果: 11111111111111111111111111101111 ...

  5. C# 常见集合之前的转换

    1,从System.String[]转到List<System.String> System.String[] str={"str","string" ...

  6. scala vs java 相同点和差异

    本贴是我摘抄自国外网站,用作备忘,也作为分享! Similarities between Scala and Java Following are some of the major similari ...

  7. HTML 元素

    HTML 文档是由 HTML 元素定义的. HTML 元素 HTML 元素指的是从开始标签(start tag)到结束标签(end tag)的所有代码. 开始标签 元素内容 结束标签 <p> ...

  8. Saltstack系列3:Saltstack常用模块及API

    说明 salt '*' sys.list_modules #列出当前版本支持的模块 API原理是通过调用master client模块,实例化一个LocalClient对象,再调用cmd()方法来实现 ...

  9. idea系列新版注册模式

    http://idea.qinxi1992.cn/ 楼上被列入黑名单,用 http://114.215.133.70:41017/

  10. C基础--结构体成员初始化方式

    之前在linux内核代码中看到结构体成员成员初始化使用类似于.owner = THIS_MODULE, 不太见过,于是搜了个博客,分享下: 转自:http://www.cnblogs.com/Anke ...