UIKit 框架之UIAlertView

简介:

//
//  ViewController.m
//  UIAlertView
//
//  Created by City--Online on 15/5/18.
//  Copyright (c) 2015年 XQB. All rights reserved.
//
 
#import "ViewController.h"
 
@interface ViewController ()<UIAlertViewDelegate>
@property(nonatomic,strong) UIAlertView *alertView;
@property(nonatomic,strong) UIAlertView *alertView1;
@end
 
@implementation ViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    _alertView=[[UIAlertView alloc]initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil ];
//    typedef NS_ENUM(NSInteger, UIAlertViewStyle) {
//        UIAlertViewStyleDefault = 0,    无文本框
//        UIAlertViewStyleSecureTextInput, 密码样式
//        UIAlertViewStylePlainTextInput,  普通样式
//        UIAlertViewStyleLoginAndPasswordInput  两个文本框
//    };
    _alertView.alertViewStyle=UIAlertViewStyleLoginAndPasswordInput;
    //添加按钮
    [_alertView addButtonWithTitle:@"YES"];
    //获取弹出框按钮的个数并遍历
    for (int i=0; i<_alertView.numberOfButtons; i++) {
        //根据索引值获取按钮的标题
        NSLog(@"i=%d  %@",i,[_alertView buttonTitleAtIndex:i]);
    }
    
    //获取第一个其他按钮的索引
    NSLog(@"firstOtherButtonIndex=%ld",[_alertView firstOtherButtonIndex]);
    _alertView.tag=10001;
    //弹出视图
    [_alertView show];
     
    _alertView1=[[UIAlertView alloc]initWithTitle:@"提示" message:@"是否退出?" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
     
    _alertView1.tag=10002;
    [_alertView1 show];
     
}
 
//UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==10002) {
        //当我点击_alertView1的取消按钮时则同时完成点击_alertView的取消按钮
        if (buttonIndex==0) {
            //程序自动完成点击buttonIndex的button 并dismiss 整个alertView的操作
            [_alertView dismissWithClickedButtonIndex:0 animated:YES];
        }
    }
    else
    {
        if (buttonIndex==1) {
            //获取文本框
            UITextField *textField1=[_alertView textFieldAtIndex:0];
            NSLog(@"%@",textField1.text);
             
            UITextField *textField2=[_alertView textFieldAtIndex:1];
            NSLog(@"%@",textField2.text);
        }
        
    }
}
 
- (void)alertViewCancel:(UIAlertView *)alertView
{
    NSLog(@"alertViewCancel");
}
//视图显示前
- (void)willPresentAlertView:(UIAlertView *)alertView;
{
    NSLog(@"willPresentAlertView");
}
//视图显示后
- (void)didPresentAlertView:(UIAlertView *)alertView
{
    NSLog(@"didPresentAlertView");
}
//视图消失前
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
     NSLog(@"willDismissWithButtonIndex");
}
//视图消失后
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    NSLog(@"didDismissWithButtonIndex");
 
}
//第一个FirstOtherButton是否可用
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
    if (alertView.tag==10001) {
        return NO;
    }
    else
    {
        return YES;
    }
 
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
@end


 

相关文章