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 ...
随机推荐
- poj3237树链剖分边权+区间取负
树链剖分+线段树lazy-tag在树链上操作时千万不要写错.. /* 树链剖分+线段树区间变负 */ #include<iostream> #include<cstring> ...
- for循环输出数组中的分数
示例 var scores = [24, 32, 17]; // A数组 var arrayLength = scores.length;// 数组的长度 //当i<arrayLength时,可 ...
- #10 //I [HNOI/AHOI2018]毒瘤
题解: 80分做法还是听简单的 对于非树边枚举一下端点状态 然而我也不知道为什么就多t了一个点 具体实现上 最暴力的是3^n次 但是我们可以发现对于i不取,j取 i不取,j不取是可以等效成i不取,j没 ...
- python全栈开发day38-css三种引入方式、基础选择器、高级选择器、补充选择器
一.昨日内容回顾 div:分割整个网站,很多块 (1)排版标签 (2)块级标签 独占一行 可以设置高和宽,如果不设置宽高,默认是父盒子的宽 span: (1) 小区域 (2)文本标签 (3)在一行内显 ...
- P3144 关闭农场 并查集 反向
FJ和他的奶牛们正在计划离开小镇做一次长的旅行,同时FJ想临时地关掉他的农场以节省一些金钱. 这个农场一共有被用M条双向道路连接的N个谷仓(1<=N,M<=3000).为了关闭整个农场,F ...
- SystemTap - 安装
按照SystemTap Beginners Guide的Installation and Setup部分安装了SystemTap,没想到竟然还有点曲折,在这里纪录一下. 环境 Linux发行版本:Ce ...
- hibernate中多对一问题
例如User类中有个Group引用对应数据库就是groupId的外键了. 外键映射成一个类对吧. <many-to-one name="groupId" column=&qu ...
- Java并发程序设计(四)JDK并发包之同步控制
JDK并发包之同步控制 一.重入锁 重入锁使用java.util.concurrent.locks.ReentrantLock来实现.示例代码如下: public class TryReentrant ...
- 在web.xml中配置Spring的application.xml
<context-param> <!-- 请记住这里的名称不能够乱写,必须一模一样,区分大小写 java类去启动项目 contextClass--> <param-nam ...
- C中const 定义常量的使用
先看如下代码 #include <stdio.h> #include <string.h> #define ARRSIZE(a) (sizeof(a)/sizeof(a[0]) ...