IOS Using UIAlertView to show alerts
UIAlertView in other words, it's a dialog box. You want to show a message or ask user to confirm an action. UIAlertView would come in handy. Here, I create a simple project and show a alert view when suer click a button named "Show A Simple Alert View".
The implementation is in class ViewController. Below is the main code.
//
// ViewController.m
// Chapter1UIAlertView
//
// Created by Winter on 15/8/13.
// Copyright (c) 2015年 YLD. All rights reserved.
// #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor]; UIButton *buttonSimple = [[UIButton alloc] initWithFrame:CGRectMake(20.0f, 30.0f, 300.0f, 30.0f)];
[buttonSimple setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[buttonSimple setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
[buttonSimple setBackgroundColor:[UIColor orangeColor]];
[buttonSimple setTitle:@"Show A Simple Alert View" forState:UIControlStateNormal];
[buttonSimple addTarget:self action:@selector(showAlert) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:buttonSimple];
} - (void) showAlert {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Warning"
message:@"Hello"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil]; [alertView show];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
Run app you and click the button you would see below scene.
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
Now, you probably appreciate to have more button in alert view and each button would invoke different function while user clicking it. To implement this function you need to make you view controller accord with protocol UIAlertViewDelegate and implement
method alertView:clickedButtonAtIndex:. I put a label at the top of view to see what button had clicked in alert view and its button index. The cancel button's index is 0.
The ViewController.h code as below.
//
// ViewController.h
// Chapter1UIAlertView
//
// Created by Winter on 15/8/13.
// Copyright (c) 2015年 YLD. All rights reserved.
// #import <UIKit/UIKit.h> @interface ViewController : UIViewController<UIAlertViewDelegate>{
UILabel *label_;
} @end
Add below codes to viewDidLoad in ViewController.m file. These codes would create a label in top of the view and create a button next to last view element. When you click this button it would invoke method showAlertWithMoreButtons.
label_ = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 30.0f, self.view.bounds.size.width, 60.0f)];
label_.backgroundColor = [UIColor blackColor];
label_.textAlignment = NSTextAlignmentCenter;
label_.textColor = [UIColor whiteColor];
[self.view addSubview:label_]; UIButton *buttonMore = [[UIButton alloc] initWithFrame:CGRectMake(20.0f, 140.0f, 300.0f, 30.0f)];
[buttonMore setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[buttonMore setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
[buttonMore setBackgroundColor:[UIColor brownColor]];
[buttonMore setTitle:@"Show More Buttons Alert View" forState:UIControlStateNormal];
[buttonMore addTarget:self action:@selector(showAlertWithMoreButtons) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonMore];
Add method alertView:clickedButtonAtIndex: and implement it.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
label_.text =[NSString stringWithFormat:@"%@ %ld", [alertView buttonTitleAtIndex:buttonIndex], (long)buttonIndex];
}
Run the app and click button "Show More Buttons Alert View".
If you want user input some text, you can set alert view style as UIAlertViewStylePlainTextInput
- (void) showAlertTextInput {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Warning"
message:@"Do you like apple?"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", nil];
[alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
[alertView show];
}
You can get what user had input via [alertView textFieldAtIndex:0].text. I made it display in label after user finishing inputting and clicking OK.
<pre name="code" class="objc">- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
label_.text =[NSString stringWithFormat:@"%@ %ld TextFiled 0: %@", [alertView buttonTitleAtIndex:buttonIndex], (long)buttonIndex, [alertView textFieldAtIndex:0].text];
}
If you want user input some secure text, you can set alert view style as UIAlertViewStyleSecureTextInput
If you want user input login information user name and password, you can set alert view style as UIAlertViewStyleLoginAndPasswordInput. Use [alertView textFieldAtIndex:0].text
to get login text, [alertView textFieldAtIndex:1].text to get password text.
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
IOS Using UIAlertView to show alerts的更多相关文章
- ios之UIAlertView
举例: UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Default Alert View"messa ...
- 【iOS】UIAlertView 点击跳转事件
iOS 开发中,UIAlertView 经常用到.这里记录下曾用到的点击跳转事件. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@& ...
- iOS之UIAlertView的使用
UIAlertView: 1.普通使用: //普通的alert UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"title&quo ...
- iOS - 提示信息 - UIAlertView、UIActionSheet、UIAlertController的实际应用
1.UIAlertView(屏幕中央弹出框)(不需要服从代理) UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@" ...
- IOS开发:UIAlertView使用
链接地址:http://www.2cto.com/kf/201307/231841.html UIAlertView是什么就不介绍了 1.基本用法 1 UIAlertView *view = [[UI ...
- IOS对话框UIAlertView
//修改弹出对话框的样式 alertView.alertViewStyle = UIAlertViewStylePlainTextInput; //根据索引获取指定的某个文本框 [alertView ...
- iOS改变UIAlertView、UIActionSheet、UIAlertController系统字体颜色
废话不多说,直接上代码,效果是最好的说服力 1.改变UIAlertView字体颜色 [UIView appearance].tintColor = [UIColor greenColor]; 个人还是 ...
- IOS中UIAlertView(警告框)常用方法总结
一.初始化方法 - (instancetype)initWithTitle:(NSString *)title message:(NSString*)message delegate:(id /*&l ...
- IOS UIAlertView(警告框)方法总结
转自:my.oschina.net/u/2340880/blog/408873?p=1 IOS中UIAlertView(警告框)常用方法总结 一.初始化方法 - (instancetype)initW ...
随机推荐
- ubuntu预装的是vim tiny版本
可以安装vim full版本,在full版本下键盘正常,安装好后同样使用vi命令.安装vim: ubuntu预装的是vim tiny版本,而需要的是vim full版本.执安装vim full版本:$ ...
- php中按指定标识及长度替换字符的方法代码
/** * 按指定标识及长度替换字符 * @param $str * @param int $start 开始的位数 * @param int $end 后面保留的位数 * @param string ...
- 基于 OSGi 的面向服务的组件编程,helloworld
基于 OSGi 的面向服务的组件编程 OSGi(Open Services Gateway Initiative,开放服务网关协议)提供了一个面向服务组件的编程模型,基于 OSGi 编程,具有模块化, ...
- sql 根据日期模糊查询&SQL Server dateTime类型 模糊查询
曾经遇到这样的情况,在数据库的Meeting表中有PublishTime (DateTime,8)字段,用来存储一个开会时间,在存入时由于要指明开会具体时间,故格式为yyyy-mm-dd hh:mm: ...
- B 找规律
Description 对于正整数n,k,我们定义这样一个函数f,它满足如下规律f(n,k=1)=-1+2-3+4-5+6...nf(n,k=2)=-1-2+3+4-5-6...nf(n,k=3)=- ...
- HDU 3746 将字符串的全部字符最少循环2次需要添加的字符数
Sample Input3aaaabcaabcde Sample Output025 题目大意:给你一个字符串,要求将字符串的全部字符最少循环2次需要添加的字符数.例子:abcabc 已经循环2次,添 ...
- IIS 之 通过 Web.config 修改文件上传大小限制设置方法
在IIS 6.0中,不设置默认大小为4M,设置文件上传大小的方法,maxRequestLength(KB),executionTimeout(毫秒),配置如下节点: <system.web> ...
- 065 xftp的使用
1.xftp 一个基于 MS windows 平台的功能强大的SFTP.FTP 文件传输软件 2.下载安装 *3.在linux上安装服务 sudo yum install vsftp
- 在Ubuntu中成功搭建KMS服务器
介绍 基于vlmcsd搭建的KMS服务器. 根据github上的说明,这个工具是用C写的,没有任何依赖,可以直接运行.而且它横跨几乎现在所有的系统平台,如Android, FreeBSD, Solar ...
- mongoDB的配置以及运行
干嘛的:数据库,nosql(非关系型) 场景:解决大规模数据集合多重数据种类 一.mongoDb安装: 下载地址: https://www.mongodb.com/download-center ...