概述
- 警告对话框(UIAlertController)
- 等待提示view(UIActivityIndicatorView)
仔细看两种对话框的的代码,从中我们可以看出其各自的生成原理不同,从api涉及的类也可以看出一些。
示例代码
点击两个button分别弹出两种类型的dialog
先看图
示例代码
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
//警告对话框
UIAlertController *_alertController ;
//等待对话框
UIActivityIndicatorView *_activityIndicatorView;
}
@property (retain,nonatomic) UIAlertController *alertController;
@property (retain,nonatomic) UIActivityIndicatorView *activityIndicatorView;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize alertController = _alertController;
@synthesize activityIndicatorView = _activityIndicatorView;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//创建连个button
for (int i=0; i<3; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(10, 100+50*i, 300, 50);
if (i==0){
//点击显示警告对话框的button
[btn setTitle:@"show alertview" forState:UIControlStateNormal];
}else if(i==1){
//点击显示等待对话框的button
[btn setTitle:@"show loadingview" forState:UIControlStateNormal];
}else if(i==2){
[btn setTitle:@"stop loadingview" forState:UIControlStateNormal];
}
//使用tag来区别View
btn.tag = 101+i;
//添加事件(点击事件)
[btn addTarget:self action:@selector(showDialog:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
}
-(void) showDialog:(UIButton*) btn
{
if (btn.tag == 101) {
//头部title和message
_alertController = [UIAlertController alertControllerWithTitle:@"警告" message:@"再不睡觉就完了" preferredStyle:UIAlertControllerStyleAlert];
//下部按钮,按钮的排列自适应,有横向和竖向排列
[_alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"press sure");
}]];
[_alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"press cancel");
}]];
[_alertController addAction:[UIAlertAction actionWithTitle:@"哈哈哈" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"哈哈哈");
}]];
//展示使用了切换ViewController一样的方式,类似于Flutter中的方式将dialog作为一个页面覆盖在原有页面上,和Android方式不同
[self presentViewController:_alertController animated:true completion:nil];
}else if(btn.tag==102){
//大小是系统固定好的
_activityIndicatorView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(100, 300, 40, 40)];
//目前有五种style
[_activityIndicatorView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleMedium];
//与上面的警示View相比,等待View就是较传统的View,要添加到父View中然后做一个动画。
[self.view addSubview:_activityIndicatorView];
[_activityIndicatorView startAnimating];
}else if(btn.tag == 103){
if(_activityIndicatorView != nil && [_activityIndicatorView isAnimating]){
//隐藏
[_activityIndicatorView stopAnimating];
}
}
}
@end