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. 线段树解LIS

    先是nlogn的LIS解法 /* LIS nlogn解法 */ #include<iostream> #include<cstring> #include<cstdio& ...

  2. python接口自动化测试二十:函数写接口测试

    # coding:utf-8import requestsimport refrom bs4 import BeautifulSoup # s = requests.session() # 全局的s ...

  3. 如何获取JMX监控WebSphere所需的com.ibm.ws.admin.client_8.5.0等jar包

    https://blog.csdn.net/weixin_38645718/article/details/83346007

  4. [转]使用tee记录mysql client 所有的操作,十个节省时间的mysql命令

    查看:tail -f /tmp/jack.txt 一:tee方法一.配置文件在服务器上的/etc/my.cnf中的[client]加入 tee =/tmp/client_mysql.log即可. 方法 ...

  5. 【Restful】三分钟彻底了解Restful最佳实践

    REST是英文representational state transfer(表象性状态转变)或者表述性状态转移;Rest是web服务的一种架构风格;使用HTTP,URI,XML,JSON,HTML等 ...

  6. Codeforces 757D - Felicity's Big Secret Revealed

    757D - Felicity's Big Secret Revealed 题目大意:给你一串有n(n<=75)个0或1组成的串,让你划最多n+1条分割线,第一条分割线的前面和最后一条分割线的后 ...

  7. SparseArray源码解析

    转载自SparseArray源码解析 No1: Android官方推荐:当使用HashMap(K, V),如果K为整数类型时,使用SparseArray的效率更高. No2: HashMap是使用数组 ...

  8. hdu 1622 Trees on the level(二叉树的层次遍历)

    题目链接:https://vjudge.net/contest/209862#problem/B 题目大意: Trees on the level Time Limit: 2000/1000 MS ( ...

  9. pyrhon SQLite数据库

    pyrhon SQLite数据库 目录 介绍 导入模块 创建数据库/打开数据库 创建表 在表中插入行 查询/修改 删除表中的行 删除表 介绍 Python SQLITE数据库是一款非常小巧的嵌入式开源 ...

  10. P2648 赚钱

    P2648 赚钱对于不知道起点在哪里的最短路,先建立一个超级源点,然后从超级源点跑最长路,并判正环即可. #include<iostream> #include<cstdio> ...