iOS开发从入门到精通-- 警告对话框UIAlertView和等待提示器UIActivityIndicatorView

警告对话框UIAlertView和等待提示器UIActivityIndicatorView:

1.UIAlertView简单一点就是弹框

2.就是所谓的菊花转圈圈

声明:注意@interface ViewController : UIViewController<UIAlertViewDelegate>这个里面多了一个UIAlertViewDelegate代理


#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIAlertViewDelegate>{

    //定义一个警告对话框视图对象
    UIAlertView * _alertView;

    //等待提示对象
    //当下载,或者加载比较大的文件时,可以显示此控件,处于提示等待状态
    UIActivityIndicatorView * _activityIndicator;
}

@property(retain,nonatomic) UIAlertView * alertView;
@property(retain,nonatomic) UIActivityIndicatorView * activityIndicator;

@end

实现:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

//实现属性和成员变量的同步
@synthesize alertView =_alertView;
@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 if(i==1){
            [btn setTitle:@"等待指示器" forState:UIControlStateNormal];
        }

        btn.tag = 101+i;

        [btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:btn];
    }
}

-(void) pressBtn:(UIButton*) btn{
    //警告对话框创建
    //p1:对话框标题
    //p2:提示信息
    //p3:处理按钮事件的代理对象
    //p4:取消按钮的文字,默认的索引为0;
    //p5:其他按钮文字 ** otherButtonTitles:@"确定", nil]; 只有取消和确定按钮是横向排开
    // otherButtonTitles:@"确定",@"确定1",@"确定2", nil];这样子就会全部纵向排开,索引依次是1,2,3.
    //p6:...:添加其他按钮
    //nil:表示按钮添加结束
    if(btn.tag==101){
        _alertView = [[UIAlertView alloc]initWithTitle:@"警告"
                                               message:@"你的手机电量过低,请保存数据"
                                              delegate:self
                                     cancelButtonTitle:@"取消"
                                     otherButtonTitles:@"确定", nil];

        //显示对话框
        [_alertView show];
    }
    //创建等待提示器,大伙都叫他菊花
    else if(btn.tag==102){
        //创建等待提示器,宽高不可变更
        _activityIndicator =[[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(100, 300, 80, 80)];

        //设定提示的风格:小灰,小白,大白
        _activityIndicator.activityIndicatorViewStyle =UIActivityIndicatorViewStyleGray;//小灰

//        _activityIndicator.activityIndicatorViewStyle=UIActivityIndicatorViewStyleWhite;//小白
//
//        _activityIndicator.activityIndicatorViewStyle=UIActivityIndicatorViewStyleWhiteLarge;//大白

//        self.view.backgroundColor=[UIColor blueColor];

        [self.view addSubview:_activityIndicator];

        //启动动画并显示
        [_activityIndicator startAnimating];

        //停止等待动画并隐藏
//        [_activityIndicator stopAnimating];

    }
}

//当点击对话框的按钮时,调用此函数
//p1:对话框对象本身
//p2:按钮的索引
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"index=%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
时间: 2024-08-05 06:03:51

iOS开发从入门到精通-- 警告对话框UIAlertView和等待提示器UIActivityIndicatorView的相关文章

iOS开发从入门到精通--XIB使用,登陆界面小试牛刀

XIB使用,登陆界面小试牛刀 创建一个新的视图控制器,具体操作参见点击查看 在创建好的VCRoot.xib里面拖动需要的控件,并拖动给相应的控件添加属性,给登陆按钮添加事件. VCRoot.h文件里面: #import <UIKit/UIKit.h> @interface VCRoot : UIViewController //IBOutlet表示从xib中创建的 @property (weak, nonatomic) IBOutlet UITextField *mName; @propert

iOS开发从入门到精通--UIViewController基础

UIViewController基础 我们还是不用storyboard,下面我们还是以代码来逐一说明: #import <UIKit/UIKit.h> #import "AppDelegate.h" //整个app程序的主函数,入口函数 int main(int argc, char * argv[]) { //自动内存释放池 @autoreleasepool { //UIKit框架结构的启动函数 //参数一:argc,启动时带有参数的个数 //参数二:argv,参数列表

iOS开发从入门到精通--XIB使用介绍

XIB使用介绍: 首先我们删除一些不需要的东西: 然后我们创建一个新的视图控制器 红色箭头Also create XIB file要勾选上 这个时候,我们可以看到有三个文件创建成功了,其中有一个RootController.xib文件,在这个里面就看到了一个像手机一样的视图,我们可以在右边进行拖拽一些控件在上面 下面启动这个视图代码要在代理AppDelegate.m书写: 要引入#import "RootController.h" #import "AppDelegate.h

iOS开发从入门到精通--定时器UITimer和视图对象移动

定时器UITimer和视图对象移动 在ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIViewController{ //定义一个定时器对象 //可以在每隔固定的时间发送一个消息 //通过消息来调用相应的事件函数 //通过此函数可在固定时间段来完成一个根据时间间隔的任务 NSTimer * _timeView; } //定时器的属性对象 @property(retain,nonatomic) NSTi

iOS开发从入门到精通--导航控制器基础

导航控制器基础 实现上面的导航栏,左侧按钮,中间的TITLE,还有右侧的test按钮,和一个播放的按钮 重新创建一个VCRoot根视图控制器 #import "AppDelegate.h" #import "VCRoot.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaun

iOS开发从入门到精通--手动布局子视图

手动布局子视图: 下面先看下效果图,我们今天要实现的效果: 这里我们默认用storyboard启动: 首先我们要在白色的屏幕上面创建一个父视图SuperView(蓝色的背景),在父视图里面创建四个小视图(橘黄色的背景) 下面看代码, 在SuperView.h文件里面: #import <UIKit/UIKit.h> @interface SuperView : UIView{ UIView * _view01; UIView * _view02; UIView * _view03; UIVie

iOS开发从入门到精通--自动布局子视图

自动布局子视图 #import <UIKit/UIKit.h> @interface ViewController : UIViewController{ //创建父视图对象 UIView * _superView; //左上角label UILabel * _label01; //右上角label UILabel * _label02; //右下角label UILabel * _label03; //左下角label UILabel * _label04; //中间 UIView * _v

iOS开发从入门到精通--UIViewController使用

UIViewController使用:从ViewController.m跳转到ViewController02.m,ViewController.m的生命周期问题,ViewController02消失之后再显示ViewController的过程 ViewController.m里面的代码: #import "ViewController.h" #import "ViewController02.h" @interface ViewController () @end

iOS开发从入门到精通--开关按钮UISwitch控件

开关按钮UISwitch 在ViewController.h里面 #import <UIKit/UIKit.h> @interface ViewController : UIViewController{ //定义一个开关控件 //作用可以进行状态的改变 //开,关:两种状态可以切换 //所有UIKit框架库中的控件均已UI开头 //苹果官方的控件都定义在UIKit框架库中 UISwitch * _mySwitch; } @property(retain,nonatomic) UISwitch