iOS开发关于iOS9以后UIAlertController的基本使用

  

  随着iOS系统的不断升级,对于开发者的不同API 也是在不断更新,今天我们来探讨一下关于最新推出的提示框UIAlertController的使用。

  首先,在此之前,我们在一些需要用户确认的一些提示框都是使用UIAlertView 以及UIAlertSheet的,那时候大概是这样:

   

一.UIAlertView:

1:设置代理:

  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController<UIAlertViewDelegate>  
  4.   
  5. @end  

2://初始化:

 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"AlertViewTest"  

                                                   message:@"message"  

                                                  delegate:self  

                                         cancelButtonTitle:@"Cancel"  

                                         otherButtonTitles:@"OtherBtn",nil];  

3:[alert show];

 

 

二.ActionSheet:


- (void)creatActionSheet
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"title" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"其它功能", nil];
    [actionSheet showInView:self.view];
}
#pragma mark UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0:
            break;
        case 1:
            break;
        default:
            break;
    }
}
#pragma mark Use NewObject(ActionSheet)
- (void)creatActionSheet
{
    BOAlertController *actionSheet = [[BOAlertController alloc] initWithTitle:@"title" message:nil viewController:self];
    RIButtonItem *cancelItem = [RIButtonItem itemWithLabel:@"取消" action:^{
    }];
    [actionSheet addButton:cancelItem type:RIButtonItemType_Cancel];
    RIButtonItem *okItem = [RIButtonItem itemWithLabel:@"确定" action:^{
    }];
    [actionSheet addButton:okItem type:RIButtonItemType_Destructive];
    [actionSheet showInView:self.view];

三.UIAlertController:

1//初始化一个这玩意:

UIAlertController * alertView = [UIAlertController alertControllerWithTitle:@"hahaha" message:@"ssssss" preferredStyle:UIAlertControllerStyleAlert];

2//向这玩意里面加action 或者说是加按钮:

 这种就是没有点击时间的取消按钮:

    UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];

 

而如果想要点击触发后调用函数

UIAlertAction * okAction = [UIAlertAction actionWithTitle:@"haha" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        NSLog(@"hahahaha");

        [self hehe];

    }];

- (void )hehe


就好了//

3//将按钮加入到AlertController中

    [alertView addAction:cancelAction];

    [alertView addAction:okAction];

 4//显示出来这个AlertController

   [self presentViewController:alertView animated:YES completion:nil];

哈哈,大功告成,假期回来第一波,写了篇博客,好开心,希望自己的技术越来越好,也希望一些小总结,不仅仅能够帮到自己也能够服务他人。

 

 

原文地址:https://www.cnblogs.com/YaoWang/p/4863941.html