ViewController.h
// 警告对话框&等待提示器
//
// Created by mac on 2016/10/28.
// Copyright © 2016年 mac. All rights reserved.
//
#import <UIKit/UIKit.h>
// 要使用alert的代理的时候再@interface ViewController : UIViewController后面加上<UIAlertViewDelegate>
@interface ViewController : UIViewController<UIAlertViewDelegate>
{
// 定义一个警告对话框视图
UIAlertView* _aleriView;
// 定义等待提示对象
// 当下载,或加载比较大的文件时,可以显示此控件
// 处于提示等待状态
UIActivityIndicatorView* _activityIndicator;
}
@property (retain , nonatomic) UIAlertView* aleriView;
@property (retain , nonatomic) UIActivityIndicatorView* activityIndicator;
@end
ViewController.m
// 警告对话框&等待提示器
//
// Created by mac on 2016/10/28.
// Copyright © 2016年 mac. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize aleriView = _aleriView;
@synthesize activityIndicator = _activityIndicator;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
for (int i = 0; i<2; i++) {
UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(100, 100+100*i, 100, 40);
if (i == 0) {
[btn setTitle:@"警告对话框" forState:UIControlStateNormal];
}
else{
[btn setTitle:@"等待指示器" forState:UIControlStateNormal];
}
btn.tag= 101 + i;
[btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
}
-(void) pressBtn: (UIButton*) btn
{
// 创建警告对话框
if(btn.tag == 101)
{
// 创建警告对话框
// p1:对话框标题
// p2:提示信息
// p3:处理按钮时间的代理对象
// p4:取消按钮的文字
// p5:其他按钮的文字
// p6.....:添加其他按钮
// Plast:表示按钮添加结束
_aleriView = [[UIAlertView alloc] initWithTitle:@"警告"
message:@"你的手机电量过低,即将关机,请保存好数据"
delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:@"OK", @"as",@"sad",nil];
// 显示对话框
[_aleriView show];
}
// 创建等待提示器
else if (btn.tag == 102)
{
// 宽度高度不可变更
_activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(100, 300, 80, 80)];
// 设置提示风格:小灰,小白,大白
_activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
//
[self.view addSubview:_activityIndicator];
// 启动动画并且显示
[_activityIndicator startAnimating];
// 停止等待动画并隐藏
// [_activityIndicator stopAnimating];
}
}
// 当点击对话框的按钮时,调用此函数
// p1:对话框对象本身
// p2:按钮的索引
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"indes = %ld/n",buttonIndex);
}
// 对话框消失,此函数调用
-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSLog(@"即将消失");
}
// 对话框已经消失时,调用此函数
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSLog(@"已经消失");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end