说明
UIAlertView 在iOS9.0已被废弃,应使用UIAlertController代替。
头文件
#import <UIKit/UIKit.h>
@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<count; i++){
UIAlertAction* action = [UIAlertAction actionWithTitle:arr[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//点击事件输出文本
NSLog(@"%@",arr[i]);
}];
[_ac addAction:action];
}
//展示弹框view
[self presentViewController:_ac animated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
网友评论