alertView中的属性和方法的讲解

来源:互联网 发布:和女生网络聊天技巧 编辑:程序博客网 时间:2024/06/11 06:09

//

//  ViewController.m

//  alertView

//

//  Created by 朱敏 on 15/7/14.

//  Copyright © 2015 helinyu. All rights reserved.

//


#import "ViewController.h"


@interface ViewController () <UIAlertViewDelegate>


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

   


  UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"标题"message:@"message"delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"其他",nil];

    alert.delegate =self;

    

    

    NSString * abutton = [alertbuttonTitleAtIndex:1];//获取button的索引名字,eg:索引1其他

    NSLog(@"abutton is : %@",abutton);

    NSLog(@"cancancelButtonIndex:%ld",alert.cancelButtonIndex);//获取取消按钮的索引,默认为0设置将会改变

    

   NSInteger * number = [alertaddButtonWithTitle:@"选择"];//增加一个按钮,返回的是按钮的索引

    NSLog(@"number is  : %ld",number);


    NSLog(@"numberOfButtons is :%ld",alert.numberOfButtons);//按钮的数目

    

    NSLog(@"firstOtherButtonIndex is :%ld",alert.firstOtherButtonIndex);

    //显示其他按钮中的第一个下标

   

    NSLog(@"alert.visible is :%d",alert.visible);

//    alert.visible = NO;

  

   // - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated;


   [alert dismissWithClickedButtonIndex:1animated:YES];//这个方法好像没有起到作用

    //解除所以指定的按钮,当点击的时候,也就是不起到作用


    // Alert view style - defaults to UIAlertViewStyleDefault

//    @property(nonatomic,assign) UIAlertViewStyle alertViewStyle NS_AVAILABLE_IOS(5_0);

//    alert.alertViewStyle = UIAlertViewStyleDefault; //默认的,没有UItextfield

//    alert.alertViewStyle = UIAlertViewStyleSecureTextInput;//密码的输入UitextField

//    alert.alertViewStyle = UIAlertViewStylePlainTextInput;//设置一个空白的UITextField

    alert.alertViewStyle =UIAlertViewStyleLoginAndPasswordInput;//用户和密码

   

    

    

    // Retrieve a text field at an index

    // The field at index 0 will be the first text field (the single field or the login field), the field at index 1 will be the password field. */

//    - (nullable UITextField *)textFieldAtIndex:(NSInteger)textFieldIndex NS_AVAILABLE_IOS(5_0);

            UITextField * atextfield0 = [alerttextFieldAtIndex:0];

            UITextField * atextfield1 = [alerttextFieldAtIndex:1];

//          UITextField * atextfield2 = [alert textFieldAtIndex:2];

//          UITextField * atextfield3 = [alert textFieldAtIndex:3];

    //也就是alertView最多也就是只能够增加两个UITextField


    //增加的时候记得设置alertView的样式

    UILabel * label =[[UILabelalloc]initWithFrame:CGRectMake(0,0,50,20)];

    label.text  =@"hello";

    

    [alert addSubview:label];

    

    

    [alert show];


}


#pragma mark <UIAlertViewDelegate>


//监听点击按钮事件

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndexNS_DEPRECATED_IOS(2_0,9_0)

{

    NSLog(@"buttonIndex is : %ld",(long)buttonIndex);//监听点击当时的代理方法,

                                                       //点击之后按钮将会消除,就会被消除按钮监听

    

   if(buttonIndex == alertView.cancelButtonIndex)

   {

       NSLog(@"cancelButtonIndex : %ld",(long)alertView.cancelButtonIndex);

       

   }else{

        NSLog(@"no cancelButtonIndex : %ld",(long)buttonIndex);

   }


}


//可以根据打印的结果

//2015-07-14 09:37:46.163 alertView[23448:742979] buttonIndex is : 1

//2015-07-14 09:37:46.163 alertView[23448:742979] willDismissWithButtonIndex : 1

//2015-07-14 09:37:46.581 alertView[23448:742979] didDismissWithButtonIndex: 1


//事件为:点击alertView之外的地方,alertView将会显示

- (void)alertViewCancel:(UIAlertView *)alertViewNS_DEPRECATED_IOS(2_0,9_0)

{

    //这个方法应该是处理程序异常关闭的这个alertView之后调用的方法。????

    NSLog(@"alertViewCancel");

}


//alertView将要显示和显示后的监听方法

- (void)willPresentAlertView:(UIAlertView *)alertViewNS_DEPRECATED_IOS(2_0,9_0)

{

    NSLog(@"willPresentAlertView");

}// before animation and showing view


- (void)didPresentAlertView:(UIAlertView *)alertView NS_DEPRECATED_IOS(2_0,9_0)

{

    NSLog(@"didPresentAlertView");

}// after animation


//这下面两个方法是监听上面的dismissWithClickedButtonIndex:方法,

//同时监听点击之后,按钮将会消除,所以也会监听点击之后的操作

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex NS_DEPRECATED_IOS(2_0,9_0)

{

    NSLog(@"willDismissWithButtonIndex : %ld",buttonIndex);

}// before animation and hiding view


- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex NS_DEPRECATED_IOS(2_0,9_0)

{

    NSLog(@"didDismissWithButtonIndex: %ld",buttonIndex);

}// after animation


// Called after edits in any of the default fields added by the style

//在任何默认字段添加的样式中调用后调用

//其他中的按钮第一个alertView应该启动

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView NS_DEPRECATED_IOS(2_0,9_0)

{

    NSLog(@"alertViewShouldEnableFirstOtherButton");

    returnYES;

}


//https://github.com/wimagguc/ios-custom-alertview自定义alertView

//其中UIActionSheet和UIalertView中的方法原理dou都是一个样。

@end

0 0
原创粉丝点击