UIAlertView使用详解

Ios中为我们提供了一个用来弹出提示框的类 UIAlertView,他类似于javascript中的alert 和c#中的MessageBox();

UIAlertView 继承自 UIView (@interface UIAlertView : UIView )

一、简单的初始化一个UIAlertView 对象。

UIAlertView* alert = [[UIAlertView alloc] init];

激活 alert ,让它显示。

[alert show];

结果将如下:

这样虽然出现了一个提示框,但是太不过友好,让人根本无法使用。

二,带有button的提示框。

UIAlertView 里面包含了另外一种用来初始化的方法。

- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id /*<UIAlertViewDelegate>*/)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION;

带有一个button。

UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"简单的提示框" message:@"simple alert" delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];

[alert show];

带有多个button

UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"简单的提示框" message:@"simple alert" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:@"Button1",@"Button2",@"Button3" ,nil];

[alert show];

要想处理多个button点击之后做出不同响应,那么必须让当前控制器类遵循 UIAlertViewDelegate 协议。

UIAlertViewDelegate 里面包含了一个方法(- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;),用来触发在点击button之后的操作,判断是哪一个button 有两种方式,一种是更具button 的索引,

另外一种是button的title。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

NSString* buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];

if([buttonTitle isEqualToString:@"Button1"]){

userOutput.text = buttonTitle;

}if([buttonTitle isEqualToString:@"Button2"]){

userOutput.text = buttonTitle;

}if([buttonTitle isEqualToString:@"Button3"]){

userOutput.text = buttonTitle;

}if([buttonTitle isEqualToString:@"ok"]){

userOutput.text = buttonTitle;

}

}

三 、给提示框添加输入框,最经典的案例,appstore 下载的时候输入密码、

首先,初始化UITextField

userInput = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 70.0, 260.0, 25.0)];

[userInput setBackgroundColor:[UIColor whiteColor ]];

将userInput  添加在 alert上,

[alert addSubview:userInput];

- (IBAction)btnWithTextField:(id)sender {

UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Please Enter Your Email Address" message:@"simple alert" delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];

userInput = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 70.0, 260.0, 25.0)];

[userInput setBackgroundColor:[UIColor whiteColor ]];

[alert addSubview:userInput];

[alert show];

[alert release];

[userInput release];

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

NSMutableString* buttonTitle = [NSMutableString stringWithString:[alertView buttonTitleAtIndex:buttonIndex]];

if([buttonTitle isEqualToString:@"Button1"]){

userOutput.text = buttonTitle;

}if([buttonTitle isEqualToString:@"Button2"]){

userOutput.text = buttonTitle;

}if([buttonTitle isEqualToString:@"Button3"]){

userOutput.text = buttonTitle;

}if([buttonTitle isEqualToString:@"ok"]){

[buttonTitle appendString:userInput.text];

userOutput.text = buttonTitle;

}

}

转载于:https://www.cnblogs.com/wsq724439564/p/3316902.html

Objective-C ,ios,iphone开发基础:UIAlertView使用详解相关推荐

  1. [置顶] Objective-C ,ios,iphone开发基础:命名规范

    命名规范:http://bukkake.iteye.com/blog/695492  点击打开链接 转载于:https://www.cnblogs.com/pangblog/p/3292256.htm ...

  2. Objective-C ,ios,iphone开发基础:NSDictionary(字典) 和 NSMutableDictionary

    NSDictionary(字典),NSDictionary类似于 .net中的parameter,l类似于java中的map. 通过唯一的key找到对应的值,一个key只能对应一个只,而多个key可以 ...

  3. [置顶] Objective-C,/,ios,/iphone开发基础:分类(category,又称类别)

    在c++中我们可以多继承来实现代码复用和封装使程序更加简练.在objective-c中只能单继承,不能多继承,那么除了协议protocol之外,我们可以实现类似多继承的一个方法就是,分类(catego ...

  4. Objective-C ,ios,iphone开发基础:picker控件详解与使用,(实现省市的二级联动)

    第一步:新建一个单视图(single view)的工程, 命名为pickerTest,不要勾选下面两个选项,第一个是新版本里面的,第二个是单元测试,现在用不着. 点击next  ->creat之 ...

  5. Objective-C ,ios,iphone开发基础:ios数据库(The SQLite Database),使用终端进行简单的数据库操作...

    SQLite  是一个轻量级的免费关系数据库.SQLite最初的设计目标是用于嵌入式系统,它占用资源非常少,在嵌入式设备中,只需要几百K的内存就够了,可以在(http://www.sqlite.org ...

  6. 移动开发:iphone开发之触摸事件详解

    转:http://blog.sina.com.cn/s/blog_8988732e01012eaf.html iPhoneOS中的触摸事件基于多点触摸模型.用户不是通过鼠标和键盘,而是通过触摸设备的屏 ...

  7. IOS UI开发基础之超级猜图完整版本-08

    IOS UI开发基础之超级猜图完整版本-08 // // ViewController.m // 09-超级猜图 // // Created by 鲁军 on 2021/1/31. //#import ...

  8. Objective-c/iOS/iphone开发视频教程迅雷/高速下载

    转自:http://www.lwxshow.com/videos/objective-c-ios-iphone-development-video-tutorials-download-now-rea ...

  9. 新增迅雷下载地址:Objective-c/iOS/iphone开发视频教程迅雷/高速下载

    转自:http://www.lwxshow.com/archives/397 2012-4-10更新 大家访问 http://www.lwxshow.com/archives/397 看到一个2012 ...

最新文章

  1. 计算机基础知识综合试卷一,计算机基础知识试题及答案a
  2. Python爬虫实战,pyecharts模块,Python数据分析告诉你闲鱼上哪些商品抢手~
  3. 论学好Linux系统的超级重要性
  4. micronet 测试
  5. linux 统计日志最多的ip,统计nginx日志里访问次数最多的前十个IP
  6. math标准库函数----python
  7. CSS padding
  8. 将 Observable.pipe 的输入参数手动分解
  9. 为什么做了梦第二天想不起来_转告父母!为什么有人睡觉爱把脚伸到被子外面?其实与身体状况有关…切勿忽视...
  10. Integer String int 相互转化
  11. android 语音搜索动画,Android自定义控件实现UC浏览器语音搜索效果
  12. 批量生成6位数字_太准了,你是Excel高手or小白看这6个操作就知道了
  13. 《算法图解》第二章笔记与课后练习_选择排序算法
  14. 如果要在mFC客户区添加控件怎么办
  15. 【渝粤教育】广东开放大学 操作系统原理与应用 形成性考核 (46)
  16. 基于 display 和 javaScript 封装一个页面布局小插件
  17. paip.c#.net 设置窗体关闭提醒
  18. 相机标定示例(OpenCV /C++ /matlab工具箱TOOLBOX_calib)
  19. Tapestry 4.1.3学习心得
  20. 微信公众号项目笔记 二

热门文章

  1. 如何编写一个d.ts文件
  2. How browsers work
  3. EBS R12.1安装中文补丁包BUG:FAILED: file XLIFFLoader.class on worker [X]
  4. 二、十六进制数互相转换
  5. 《Unix网络编程(第3版)》代码编译的一些问题
  6. 问村民一个什么问题就能决定走哪条路?
  7. 从文件中读取并进行树的存储_数据库中的面试题你能接几招
  8. 聚类分析:1.相似性测度
  9. TokenInsight:反映区块链行业整体表现的TI指数较昨日同期上涨1.37%
  10. SAP License:SAP订单的归档及删除