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的更多相关文章

  1. ios之UIAlertView

    举例: UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Default Alert View"messa ...

  2. 【iOS】UIAlertView 点击跳转事件

    iOS 开发中,UIAlertView 经常用到.这里记录下曾用到的点击跳转事件. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@& ...

  3. iOS之UIAlertView的使用

    UIAlertView: 1.普通使用: //普通的alert UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"title&quo ...

  4. iOS - 提示信息 - UIAlertView、UIActionSheet、UIAlertController的实际应用

    1.UIAlertView(屏幕中央弹出框)(不需要服从代理) UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@" ...

  5. IOS开发:UIAlertView使用

    链接地址:http://www.2cto.com/kf/201307/231841.html UIAlertView是什么就不介绍了 1.基本用法 1 UIAlertView *view = [[UI ...

  6. IOS对话框UIAlertView

    //修改弹出对话框的样式 alertView.alertViewStyle = UIAlertViewStylePlainTextInput; //根据索引获取指定的某个文本框 [alertView ...

  7. iOS改变UIAlertView、UIActionSheet、UIAlertController系统字体颜色

    废话不多说,直接上代码,效果是最好的说服力 1.改变UIAlertView字体颜色 [UIView appearance].tintColor = [UIColor greenColor]; 个人还是 ...

  8. IOS中UIAlertView(警告框)常用方法总结

    一.初始化方法 - (instancetype)initWithTitle:(NSString *)title message:(NSString*)message delegate:(id /*&l ...

  9. IOS UIAlertView(警告框)方法总结

    转自:my.oschina.net/u/2340880/blog/408873?p=1 IOS中UIAlertView(警告框)常用方法总结 一.初始化方法 - (instancetype)initW ...

随机推荐

  1. 神经网络,前向传播FP和反向传播BP

    1 神经网络 神经网络就是将许多个单一“神经元”联结在一起,这样,一个“神经元”的输出就可以是另一个“神经元”的输入.例如,下图就是一个简单的神经网络: 我们使用圆圈来表示神经网络的输入,标上“”的圆 ...

  2. linux 卸载自带apache httpd 安装apache httpd

    一.卸载自带apache httpd 1.关闭httpd服务:/etc/init.d/httpd stop 2.列出相关程序包:rpm -qa|grep httpd 3.卸载命令:rpm -e --n ...

  3. PE文件版本那些事儿

    发现文件的版本号很有意思,win7下右键属性显示两个版本号,分别是File Version 和 Product version.但使用vs编辑版本资源里面却有四处版本号,如下: 发现有以下区别,上面为 ...

  4. 【C++ Primer | 15】C++虚函数表剖析①

    概述 为了实现C++的多态,C++使用了一种动态绑定的技术.这个技术的核心是虚函数表(下文简称虚表).本文介绍虚函数表是如何实现动态绑定的. C++多态实现的原理: •  当类中声明虚函数时,编译器会 ...

  5. thinkphp搭建后台品字形框架页面

    页面分为三个部分 head,left,right共同组成了index 在indexController中 function Index(){ $this->display(); } //展现后腰 ...

  6. [九省联考2018]一双木棋chess

    题解: 水题吧 首先很显然的是状压或者搜索 考虑一下能不能状压吧 这个东西一定是长成三角形的样子的 所以是可以状压的 相邻两位之间有几个0代表他们差几 这样最多会有2n 然后就可以转移了 由于之前对博 ...

  7. Codeforces Round #369 (Div. 2)-C Coloring Trees

    题目大意:有n个点,由m种颜料,有些点没有涂色,有些点已经涂色了,告诉你每个点涂m种颜色的价格分别是多少, 让你求将这n个点分成k段最少需要多少钱. 思路:动态规划,我们另dp[ i ][ j ][ ...

  8. 既有e^x又有sinx或cosx的积分题的解法

    楼上三位,一致对e^x情有独钟,他们都是对的.通常,这类题既有e^x又有sinx或cosx的积分题,一般的解法是:1.选定e^x,或选定sinx.cosx,就得“从一而终”,用分部积分的方法计算,   ...

  9. mac电脑对ntfs格式硬盘进行写操作(简单说就是向ntfs硬盘拷贝东西)

    使用mac电脑的童鞋应该都会遇到一个问题: 对ntfs格式的优盘或硬盘(移动硬盘默认ntfs)只能读不能写,也就是只能拷贝出数据,却没法拷贝数据到移动硬盘中. 下面是参考自网上的一种方法,无需第三方软 ...

  10. TensorFlow 核心——数据流图

    1 计算模型 -- 计算图(Graph) 更多参考:数据流图 TensorFlow 中的所有计算都会被转化为计算图上的节点.TensorFlow 是一个通过计算图的形式来表述计算的编程系统.Tenso ...