IOS UIAlertView 提示视图

一 UIAlertView 简介

如果需要弹出讯息让用户确认,或者要求用户输入帐户密码,其他本文,则可用用UIAlertView。

二 UIAlertView 创建

    /**
     1.创建 UIAlertView
     
     title                  提示视图标题,比如 告警、提示、异常
     message                用户看的实际讯息
     delegate               可选参数,传递委托对象给提示视图,当视图状态变更时,委托对象会被通知。传递的参数对象必须实现 UIAlertViewDelegate 协定
     cancelButtonTitle      可选参数,这个字符串符会显示在提示示视图的取消按钮上。     
     otherButtonTitles      可选参数,若你希望提示示视图出现其他按钮,只要传递标题参数,此参数需用逗号分隔,用 nil 做结尾。
     
     */
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title"
                                                        message:@"Message"
                                                       delegate:nil
                                               cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];

三 设置样式

  /**
     2. 设置样式
     UIAlertViewStyleDefault = 0,            默认,没有输入框
     UIAlertViewStyleSecureTextInput,        提示视图中添加密码框
     UIAlertViewStylePlainTextInput,         提示视图中添加输入框
     UIAlertViewStyleLoginAndPasswordInput   登录和密码框
     
     */
    [alertView setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];

四 展示

    [alertView show];

五 监听点击,并获取用户的输入

如果要监听用户的点击和获取用户输入,需要实现UIAlertViewDelegate 协议,协议中的alertView:clickedButtonAtIndex 方法可以得到用户在提示视图上所按的按钮,按钮的索引值会被储存在变量 clickedAtIndex 中

/**
 *  监听点击
 *
 *  @param alertView   <#alertView description#>
 *  @param buttonIndex <#buttonIndex description#>
 */
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
    
    // 判断点击
    if ([buttonTitle isEqualToString:@"Cancel"]){
        NSLog(@"User pressed the Cancel button.");
    }
    else if ([buttonTitle isEqualToString:@"Ok"]){
        NSLog(@"User pressed the Ok button.");
    }
    
    
    //接受输入类容
    //textFieldAtIndex 获取对应位置的UITextField
    UITextField *textField = [alertView textFieldAtIndex:0];
    NSLog(@"%@",textField.text);
    
    UITextField *textField2 = [alertView textFieldAtIndex:1];
    NSLog(@"%@",textField2.text);
}

六 完整代码

#import "ViewController.h"

@interface ViewController ()<UIAlertViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //UIAlertView 作用
    //如果需要弹出讯息让用户确认,或者要求用户输入帐户密码,其他本文,则可用用UIAlertView
    
    /**
     1.创建 UIAlertView
     
     title                  提示视图标题,比如 告警、提示、异常
     message                用户看的实际讯息
     delegate               可选参数,传递委托对象给提示视图,当视图状态变更时,委托对象会被通知。传递的参数对象必须实现 UIAlertViewDelegate 协定
     cancelButtonTitle      可选参数,这个字符串符会显示在提示示视图的取消按钮上。     
     otherButtonTitles      可选参数,若你希望提示示视图出现其他按钮,只要传递标题参数,此参数需用逗号分隔,用 nil 做结尾。
     
     */
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title"
                                                        message:@"Message"
                                                       delegate:nil
                                               cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
    
    
    
    /**
     2. 设置样式
     UIAlertViewStyleDefault = 0,            默认,没有输入框
     UIAlertViewStyleSecureTextInput,        提示视图中添加密码框
     UIAlertViewStylePlainTextInput,         提示视图中添加输入框
     UIAlertViewStyleLoginAndPasswordInput   登录和密码框
     
     */
    [alertView setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
    
    
    
    
    
    /**
     3. 监听点击
     
     如果要监听用户的点击需要实现UIAlertViewDelegate 协议,协议中的alertView:clickedButtonAtIndex 方法可以得到用户在提示视图上所按的按钮,按钮的索引值会被储存在变量 clickedAtIndex 中
     */
    [alertView setDelegate:self];
//    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title"
//                                                        message:@"Message"
//                                                       delegate:self
//                                              cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];

    
    /**
     4. 接受AlertView 输入类容
     */
    
    
    
    //展示
    [alertView show];
    

}

/**
 *  监听点击
 *
 *  @param alertView   <#alertView description#>
 *  @param buttonIndex <#buttonIndex description#>
 */
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
    
    // 判断点击
    if ([buttonTitle isEqualToString:@"Cancel"]){
        NSLog(@"User pressed the Cancel button.");
    }
    else if ([buttonTitle isEqualToString:@"Ok"]){
        NSLog(@"User pressed the Ok button.");
    }
    
    
    //接受输入类容
    UITextField *textField = [alertView textFieldAtIndex:0];
    NSLog(@"%@",textField.text);
    
    UITextField *textField2 = [alertView textFieldAtIndex:1];
    NSLog(@"%@",textField2.text);
}

@end
时间: 2024-08-02 14:47:35

IOS UIAlertView 提示视图的相关文章

iOS UIAlertView中UIActivityindicatorView风火轮提示加载等待

参考:http://stackoverflow.com/questions/18729220/uialertview-addsubview-in-ios7 1.SignInViewController.h #import <UIKit/UIKit.h> @interface SignInViewController : UIViewController<UIAlertViewDelegate>{ UIAlertView *remoteAlertView; } @end 2.Sign

iOS开发系列--视图切换

概述 在iOS开发中视图的切换是很频繁的,独立的视图应用在实际开发过程中并不常见,除非你的应用足够简单.在iOS开发中常用的视图切换有三种,今天我们将一一介绍: UITabBarController UINavigationController 模态窗口 UITabBarController iOS三种视图切换的原理各不相同: UITabBarController:以平行的方式管理视图,各个视图之间往往关系并不大,每个加入到UITabBarController的视图都会进行初始化即使当前不显示在

关于iOS中提示框的使用

关于iOS中提示框的使用在iOS8的SDK里,已经对提示框进行了更改,现在属于 UIAlertController,其接口如下声明NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertController : UIViewController,不再是一个UIView. 具体的使用如下,希望对某些朋友有帮助.#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0 //判断你当前的iOS SDK所支持的最大iOS系

IOS之表视图单元格删除、移动及插入

1.实现单元格的删除,实现效果如下 Cpp代码   - (void)viewDidLoad { [super viewDidLoad]; //设置导航栏 self.editButtonItem.title = @"编辑"; self.navigation.rightBarButtonItem = self.editButtonItem; [self initTableViewData]; // Do any additional setup after loading the view

ios 获取当前视图第一响应者

Football on Table 题意:一些杆上有人,人有一个宽度,然后现在有一个球射过去,要求出球不会碰到任何人的概率 思路:计算出每根杆的概率,之后累乘,计算杆的概率的时候,可以先把每块人的区间长度再移动过程中会覆盖多少长度累加出来,然后(1?总和/可移动距离)就是不会碰到的概率 代码: #include <stdio.h> #include <string.h> #include <math.h> const double eps = 1e-8; int t,

iOS-设置视图的边框

iOS-设置视图的边框 效果图: 具体代码: //创建视图 UIView * view=[[[UIView alloc]init]autorelease]; view.frame=CGRectMake(0, 0, 270, 250); view.backgroundColor=[UIColor whiteColor]; view.center=self.view.center; [self.view addSubview:view]; //view的边框 CALayer * layer=[vie

[转]iOS开发之视图控制器(UIViewController)

视图控制器应该在MVC设计模式中扮演控制层(C)的角色,UIViewController的职责对内管理与之关联的View,对外跟其他UIViewController通信和协调.一个视图控制器管理一个视图(它可以有子视图),其view属性指向它所管理的视图.UIViewController类可以有子类,可以使用一个系统的UIViewController子类或者直接自己创建一个UIViewController的子类. 使用代码创建控制器和视图. 开始创建一个基于窗口的Empty Applicatio

Swift - iOS中各种视图控制器(View Controller)的介绍

在iOS中,不同的视图控制器负责不同的功能,采用不同的风格向用户呈现信息.下面对各个视图控制器做个总结: 1,标准视图控制器 - View Controller 这个控制器只是用来呈现内容.通常会用来作为子类,以向屏幕中添加逻辑. 2,导航控制器 - Navigation Controller 这个控制器呈现一个视图控制器的栈,应用程序可以在上面推入更多的视图控制器. 当新视图推入栈,或旧视图弹出栈时,导航控制器会以动画的形式(比如卷动)显示隐藏这些视图. 使用样例:系统“设置”应用程序 3,表

ios:设置视图背景图片的方法

1. 使用一个UIImageView实例做子视图,并且放最后面UIImageView *customBackgournd = [UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.jpg"]];self.background = customBackground;[customBackground release]; [self addSubview:background];[self sendSubVie