iOS - 消息弹框Alert

说明

UIAlertView 在iOS9.0已被废弃,应使用UIAlertController代替。

头文件

#import 

@interface ViewController : UIViewController{
    //UIAlertView* v;
    //定义弹框
    UIAlertController* _ac;
}
//定义弹框属性
@property(nonatomic, retain)UIAlertController * ac;
@end

源文件

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize ac = _ac;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(100, 100, 100, 100);
    btn.backgroundColor = [UIColor orangeColor];
    [btn setTitle:@"弹框" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}

-(void) onClick:(UIButton*) btn{
    NSLog(@"点击了%@",btn.currentTitle);
    [self alert];
}

-(void) alert{
    //UIAlertView已在iOS9废弃,应使用UIAlertController
    //构建UIAlertController,基于标题、消息、样式
    _ac = [UIAlertController alertControllerWithTitle:@"title" message:@"msg" preferredStyle:UIAlertControllerStyleAlert];
    //构建一个字符串数组
    NSArray* arr = @[@"a",@"b",@"c",@"d",@"e"];
    //获得数组长度
    NSInteger count = [arr count];
    //遍历数组,创建每一个UIAlertAction*,即:元素、动作
    //弹框添加刚创建的UIAlertActon
    for(int i=0; i

你可能感兴趣的:(iOS - 消息弹框Alert)