补充UIAlertView的一些知识,包括UIAlertViewStyleLoginAndPasswordInput和代理方法

来源:互联网 发布:mac 照片和文稿 编辑:程序博客网 时间:2024/06/08 08:19

补充说明:上一节在加法计算器中提到系统自动提示框,其实就是UIAlertView这个控件,这一节主要

UIAlertView的属性和里面的代理方法进行说明,其中可能有其他没有讲到方法,可以试着自己去框架中查看并

试着使用一下,具体看看效果,IT行业都有这么一句话,不知道的话就试试,错了又不会怀孕;


#import "ViewController.h"


@interface ViewController ()<UIAlertViewDelegate>  //使用代理方法就必须遵循UIAlertViewDelegate协议


@property (weak, nonatomic) IBOutletUILabel *label;


@end

@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

//UIAlertView的作用

    //自动弹出框,提示用户,以及让用户输入一些数据及文本时,可用UIAlertView

    //1.弹出框信息

    UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"游戏结束!"message:@"你失败了!"delegate:selfcancelButtonTitle:@"继续游戏!"otherButtonTitles:@"退出游戏",nil];

    //2.添加弹出框风格,这些风格都是苹果提供好的,我们可以直接使用

    alert.alertViewStyle =UIAlertViewStyleLoginAndPasswordInput;

    //3.设置UIAlertView弹出框的颜色

    [alert setTintColor:[UIColorredColor]];

    [alert setDelegate:self];  //设置代理者,一般都是self,当然还可以在storyboard中连线

    [alert show]; //展示UIAlertView提示框

}

//UIAlertView代理方法 这个方法的功能是监听用户点击,获取点击控件的nsstring


-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

    NSString *newString = [alertView buttonTitleAtIndex:buttonIndex];

    if ([newString isEqualToString:@"继续游戏!"]) {

        NSLog(@"你点击了继续游戏");  //打印验证监听

    }else if([newStringisEqualToString:@"退出游戏"]){

        NSLog(@"你点击了退出游戏");

    }

}


注意点: 1.UIAlertView的显示内容在创建的时候就要设置好,设置好后执行[alert show]这个方法

       2.使用代理方法,控制器必须遵循UIAlertViewDelegate协议

       3.设置代理者为self


0 0