iOS入门-12Dialog提示框

概述

  • 警告对话框(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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值