17UIAlertView_UIActionSheet_UIAlertController_storyBoard_Thread

大纲

一、UIAlertView
项目:UIAlertView0314
步骤: 1.创建AlertView 2.实现可选代理方法 3.show
设置代理:①进入AlertView,复制可选代理方法②实现代理方法

二、UIActionSheet
项目:UIActionSheet0314
步骤: 1.创建AlertSheet 2.实现可选代理方法 3.展示(showInView:self.view)

三、UIAlertController
项目:UIAlertController0314
步骤:
1.创建AlertController
2.创建AlertAction
3.将Action添加到Controller
4.展示Controller(模态弹出self presentViewController:)

四、添加文本框TextField

  1. UIAlertView中添加文本框
    项目:UIAlertView_TextField0314
    步骤:
    1.创建AlertView
    2.设置样式(alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;)
    3.获取文本框
    4.show

2.UIAlertController添加文本框
项目:UIAlertController_TextField0314
步骤:
1.创建AlertController
2.创建AlertAction
3.将Action添加到Controller
4.添加文本框(调用Controller的方法addTextFieldWithConfigurationHandler:)
5.获取文本框数组(alertController.textFields)
6.展示UIAlertController(模态弹出self presentViewController:)

五、storyBoard的下一页
项目:StoryBoard&XIB0314
步骤: 1.找到UIStoryboard 2.通过storyBoard实例化ViewController 3.找window 4.设置根视图控制器

六、线程Thread
项目:Thread0314
1.程序、进程、线程
程序:(静态的概念)实现某一功能的一组指令集合。程序是指令、数据及其组织形式的描述。
进程:(动态的概念)正在进行的程序。进程是程序的实体。
线程:程序执行的顺序流。线程是进程中的一个实体。
注:一个进程中至少有一个线程,即主线程。

2.线程就像现实生活中的路
苹果公司规定:①只能在主线程刷新UI ②分线程处理耗时任务
3.多线程
优点:
1.防止线程阻塞,提高程序运行效率。
2.(多核处理器条件下)可以将线程分配给多核处理器,实现真正意义的并行计算。
3.主线程处理UI,分线程处理耗时任务,使程序运行更流畅,提升用户体验。
缺点:耗费内存

正文

一、UIAlertView
项目:UIAlertView0314
步骤: 1.创建UIAlertView 2.实现代理方法 3.show
设置代理:①进入UIAlertView,复制可选代理方法②实现代理方法
源码:

@interface ViewController ()(要提示代理方法,就在interface括号后添加协议)
//删除
- (IBAction)deleteClick:(UIButton *)sender
{
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"删除" message:@"是否确定删除?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    
    [alertView show];
    alertView.tag = 1;
}//退出
- (IBAction)exitClick:(UIButton *)sender
{
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"退出" message:@"是否确定退出?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"退出", nil];
    [alertView show];
    alertView.tag = 2;
}
//实现协议方法
//参数:
//alertView:通过tag,确定是哪个alertView
//clickedButtonAtIndex:确定是哪个按钮
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 1)
    {
        NSLog(@"删除键");
        if (buttonIndex == 0)
        {
            NSLog(@"取消,不删除。");
        }
        else
        {
            NSLog(@"确定,删除该文件。");
        }
    }
    else if (alertView.tag == 2)
    {
        NSLog(@"退出键");
        
        if (buttonIndex == 0)
        {
            NSLog(@"取消,不退出。");
        }
        else
        {
            NSLog(@"确定,退出应用程序。");
        }
    }
}

二、UIActionSheet
项目:UIActionSheet0314
步骤: 1.创建UIAlertSheet 2.实现可选代理方法 3.展示(showInView:self.view)
源码:

@interface ViewController ()
//删除
- (IBAction)deleteClick:(UIButton *)sender
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"删除" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"销毁" otherButtonTitles:@"确定", nil];
    [actionSheet showInView:self.view];
    
}
//代理可选方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"%d",buttonIndex);
}

三、UIAlertController
项目:UIAlertController0314
步骤:
1.创建UIAlertController
2.创建UIAlertAction
3.将UIAlertAction添加到UIAlertController
4.展示UIAlertController(模态弹出self presentViewController:)
源码:

#pragma mark UIAlertController
- (IBAction)deleteClick:(UIButton *)sender
{
    //UIAlertController替代了UIAlertView和UIActionSheet
    //1.创建
    //Title:控制器的标题
    //message:提示信息
    //preferredStyle:样式(对话框、列表两种样式)
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"删除" message:@"不后悔?" preferredStyle:UIAlertControllerStyleAlert];
    //2.通过实例化一个UIAlertAction的对象,给控制器添加动作按钮
    //2.1 实例化对象(按钮)
    //Title:动作按钮标题
    //style:按钮样式(就是按钮类型,是取消按钮还是其他按钮)
    //handler:(一个方法体)block块,指定点击动作按钮时,执行的事件。
    //注意:取消按钮是唯一的,否则会出现如下错误:
    //reason: 'UIAlertController can only have one action with a style of UIAlertActionStyleCancel'.
    UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击取消按钮,什么也不干!");
    }];
    UIAlertAction *alertAction2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"确定删除该文件");
    }];
    //2.2 添加
    [alertController addAction:alertAction];
    [alertController addAction:alertAction2];
    //2.3 展示(调用模态弹出)
    [self presentViewController:alertController animated:YES completion:nil];
}

四、添加文本框TextField

  1. AlertView中添加文本框
    项目:UIAlertView_TextField0314
    步骤:
    1.创建UIAlertView
    2.设置样式(alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;)
    3.获取文本框
    4.show
    源码:
//添加联系人
- (IBAction)addContact:(UIButton *)sender
{
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"添加联系人" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    //alertViewStyle:设置样式
    alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
    //获取到alertView上的文本框
    UITextField *loginTF = [alertView textFieldAtIndex:0];
    loginTF.placeholder = @"请输入用户名";
    
    UITextField *pwdTF = [alertView textFieldAtIndex:1];
    pwdTF.placeholder = @"请输入密码";
    pwdTF.secureTextEntry = YES;
    [alertView show];
}

2.AlertController添加文本框
项目:UIAlertController_TextField0314
步骤:
1.创建UIAlertController
2.创建UIAlertAction
3.将Action添加到Controller
4.添加文本框(调用Controller的方法addTextFieldWithConfigurationHandler:)
5.获取文本框数组(alertController.textFields)
6.展示UIAlertController(模态弹出self presentViewController:)

//添加联系人
- (IBAction)addContact:(UIButton *)sender
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"练系人" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertController addAction:alertAction];
#pragma mark 添加获取文本框 方法1:
    //添加文本框
    [alertController addTextFieldWithConfigurationHandler:nil];
    [alertController addTextFieldWithConfigurationHandler:nil];
    //获取文本框数组
    NSArray *tfArr = alertController.textFields;
    UITextField *userNameTF = [tfArr objectAtIndex:0];
    userNameTF.placeholder = @"请输入用户名";
    UITextField *pwdTF = [tfArr objectAtIndex:1];
    pwdTF.placeholder = @"密码";
#pragma mark 添加获取文本框 方法2:
    //添加文本框
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"请输入账号";
    }];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"请输入密码";
    }];
#pragma mark 添加获取文本框 注意:样式只能是UIAlertControllerStyleAlert,才能添加文本框
    
    //展示
    [self presentViewController:alertController animated:YES completion:nil];
}

五、storyBoard的下一页
项目:StoryBoard&XIB0314
步骤:
1.找到UIStoryboard
2.通过storyBoard实例化ViewController
3.找window
4.设置根视图控制器
源码:

//返回
- (IBAction)backClick:(UIButton *)sender
{
    //1.找到UIStoryboard
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    //2.通过storyBoard实例化ViewController
    //注意:这里的vc依然是新的对象
    ViewController *vc = [storyBoard instantiateInitialViewController];
    //3.找window
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    window.rootViewController = vc;
}

六、线程Thread
项目:Thread0314
1.程序、进程、线程
程序:(静态的概念)实现某一功能的一组指令集合。
进程:(动态的概念)正在进行的程序。
线程:程序的执行循序流。
一个进程中至少有一个线程,即主线程。
程序是指令、数据及其组织形式的描述,进程是程序的实体。
线程是进程中的一个实体。
2.线程就像现实生活中的路
苹果公司规定:
①只能在主线程刷新UI
②分线程处理耗时的任务
3.多线程
优点:
1.防止线程阻塞,提高程序运行效率。
2.在多核处理器的配合下,可以将线程分配给多核处理器,实现真正意义的并行计算。
3.主线程统一处理UI,分线程处理耗时任务,使程序运行更流畅,提升用户体验。
缺点:
1.分线程过多会耗费内存,影响程序运行
源码:

//主线程
- (void)viewDidLoad {
    [super viewDidLoad];
    //获取当前线程
    //currentThread:通过此方法输出此线程
    NSLog(@"currnetThread = %@",[NSThread currentThread]);
    //2.创建分线程
//    //2.1第一种方法:
//    //2.1.1 创建
//    NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(onOtherThread) object:nil];
//    //2.1.2 开启
//    [thread start];
    
    //2.2第二种方法:
    //2.2.1 创建(无需开启)
    //返回值为void,无需接收。
    [NSThread detachNewThreadSelector:@selector(onOtherThread) toTarget:self withObject:nil];
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.frame];
    imageView.image = [UIImage imageNamed:@"1.jpg"];
    [self.view addSubview:imageView];
}
//分线程
- (void)onOtherThread
{
    //分线程生命周期:
    //分线程中的任务结束后,周期结束。
    NSLog(@"OtherThread = %@",[NSThread currentThread]);
    //分线程加载数据
    for (int i = 0; i<10; i++)
    {
        //线程休眠
        [NSThread sleepForTimeInterval:0.1];
        NSLog(@"i = %d",i);
    }
    //数据加载完毕之后
    //从分线程中 跳回 主线程
    //waitUntilDone:
    //YES:先将主线程的任务执行完,再执行分线程中剩下的任务
    //NO:同时执行
    [self performSelectorOnMainThread:@selector(onMainThread) withObject:nil waitUntilDone:NO];
    NSLog(@"还有任务,继续在分线程执行");
}
//主线程
- (void)onMainThread
{
    for (int i = 0; i < 100; i++)
    {
        NSLog(@"刷新UI");
    }
}

你可能感兴趣的:(17UIAlertView_UIActionSheet_UIAlertController_storyBoard_Thread)