IOS课程笔记[4-5] 计算器实现与更换主题 的使用

news/2024/7/20 22:35:56 标签: ios, 笔记, cocoa

计算

控件介绍

文本输入

  • 设置键盘格式为NumberPad
  • 字符串与数字转换方法 NSInteger num2 =[str2 integerValue];

弹窗控件

  • UIAlertController 新版本弹窗
 UIAlertController *alert =    [UIAlertController alertControllerWithTitle:@"error" message:@"输入有误" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action =[UIAlertAction  actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            NSLog(@"取消");       

        }];

        [alert addAction:action];

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

  • UIAlertView IOS9之前版本
   UIAlertView errView = [[UIAlertView alloc] initWithTitle:@"error" message:message delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];

    [errView show]

实现逻辑

  NSLog(@" 点击按钮");

    //达到数字

    NSString *str1=  self.Num1Text.text;

    NSString *str2 = self.Num2Text.text;

    //转为数字

    NSInteger num1 =[str1 integerValue];

    NSInteger num2 =[str2 integerValue];

//校验

    if (num1 ==0 || num2 ==0){

        UIAlertController *alert =    [UIAlertController alertControllerWithTitle:@"error" message:@"输入有误" preferredStyle:UIAlertControllerStyleAlert];

        

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

            NSLog(@"取消");

            

        }];

        [alert addAction:action];

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

        return ;

    }

    //相加

    NSInteger result =num1+num2;

    //展示结果

    self.ResultLabel.text =[NSString stringWithFormat:@"%zd",result];

切换 主题

使用控件

  • UISwitch 开关组件
  • UISegmentedControl多选框

基本用法

-addTarget绑定点击或者切换事件

[sc addTarget:self action:@selector(changeThemeBystring:) forControlEvents:UIControlEventValueChanged];

代码实现



-(void) loadTopView{

     

    //添加文本

    UILabel *lb=[[UILabel alloc] init];

    [lb setText:@"更换主题"];

    [lb setFrame:CGRectMake(5, 0, 400, 40)];

     [self.topView addSubview:lb];

    //开关

    UISwitch *sw =[[UISwitch alloc] init];

    [sw setFrame:CGRectMake(200, 0, 200, 40)];

    [sw addTarget:self action:@selector(changeTheme:) forControlEvents:UIControlEventTouchUpInside];

    

    [self.topView addSubview:sw];

    

    

    //多选框

    UISegmentedControl *sc =[[UISegmentedControl alloc] initWithItems:@[@"yellow",@"red",@"green"]] ;

    [sc setFrame:CGRectMake(5, 55, 300, 40)];

    [sc addTarget:self action:@selector(changeThemeBystring:) forControlEvents:UIControlEventValueChanged];

    [self.topView addSubview: sc];

    

    //控件位置

    [self.topView  setCenter:CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2) ];

}

#pragma mark 更换主题

- (void)changeTheme:(UISwitch *)sw{

    if(sw.isOn){

        self.view.backgroundColor= [UIColor whiteColor];

    }else{

        self.view.backgroundColor =[UIColor darkGrayColor];

    }

}

- (void)changeThemeBystring:(UISegmentedControl  *)sg{

    NSLog(@"%@",sg);

    switch  (sg.selectedSegmentIndex){

        case 0:            

            [self changeThemeBycolor:[UIColor yellowColor]];

            break;

        case 1:

            [self changeThemeBycolor:[UIColor redColor]];

            break;

        case 2:

            [self changeThemeBycolor:[UIColor greenColor]];

            break;          

    }

}

##效果展示
在这里插入图片描述


http://www.niftyadmin.cn/n/5095538.html

相关文章

Java设计模式之组合模式

组合模式(Composite Pattern)是一种结构型设计模式,它允许将对象组合成树形结构以表示"部分-整体"的层次结构。组合模式使得客户端能够以一致的方式处理单个对象以及对象的组合。 在组合模式中,存在两种主要的对象类型…

数据库系列之MySQL中Join语句优化问题

最近使用MySQL 8.0.25版本时候遇到一个SQL问题,两张表做等值Join操作执行很慢,当对Join连接字段添加索引优化后,执行效率反而变得更差,其中的原因值得分析。因此本文介绍下MySQL中常见的Join算法,并对比使用不同Join算…

【Linux】介绍 Docker 的基本概念和优势,以及在应用程序开发中的实际应用

Docker 是一种轻量级的虚拟化技术,它基于 Linux 容器技术,能够在不同平台、不同主机上快速地运行和部署应用程序。Docker 的基本概念包括以下几点: 镜像(Image):Docker 镜像是一个只读的模板,它…

12-k8s-HPA自动扩缩容

文章目录 一、k8s弹性伸缩类型二、HPA原理三、metrics-server插件四、创建nginx提供负载测试五、部署HPA master操作即可 一、k8s弹性伸缩类型 Cluster-Autoscale: 集群容量(node数量)自动伸缩,跟自动化部署相关的,依赖iaas的弹性伸缩,主要用…

Hadoop3教程(十六):MapReduce中的OutputFormat

文章目录 (105)OutputFormat概述(106)自定义OutputFormat案例需求分析(107/108)自定义OutputFormat案例实现自定义Mapper自定义Reducer自定义OutputFormatDriver 参考文献 (105)Outp…

[题] 子矩阵的和 #二维前缀和

题目 子矩阵的和 题解 s[i][j] ( s[i - 1][j] s[i][j - 1] - s[i - 1][j - 1] ) ; 将输入后的数组进行初始化 表示以(1, 1)为左上角以(i, j)为右下角的矩阵内所有元素之和。 s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] s[x1 …

头像Css

现有如下的图标,他这外层的白色圆圈是border,你敢信 .top-bar__img {margin: 0 8px 0 5px;padding: 2px;width: 30px;height: 30px;border-radius: 100%;box-sizing: border-box;border: 1px solid #eee;vertical-align: middle; }

IntelliJ IDEA 中 Maven 相关操作详解

在这篇文章中,我们将详细探讨 IntelliJ IDEA 中 Maven 的相关操作。我们将从以下三个角度进行讲解: IntelliJ IDEA 中 Maven 插件的 "Reimport All Maven Projects" 和 "Generate Sources and Update Folders For All Projects" 按…