iOS学习笔记——第二天

iOS学习笔记——第二天

一个简易加法计算器

要求实现最基本的加法运算。
分析:该页面由三个text、两个label和一个button构成。三个text分别用来数字输入,结果输出。两个label用来安置“+”和“=”。button是“计算”按钮。
大致的界面如图
在这里插入图片描述
计算按钮的代码

- (IBAction)Calculate {
    // 1. 拿到两个文本内容
    NSString *firstStr = self.firstNumTextField.text;
    NSString *secondStr= self.secondNumTextField.text;
    // 判断
    if(firstStr.length == 0)
    {
        [self ShowInfo:@"请输入第一个数"];
        return;
    }
    if(secondStr.length == 0)
    {
        [self ShowInfo:@"请输入第二个数"];
        return;
    }
    // 2. string转成int
    NSInteger firNum = [firstStr integerValue];
    NSInteger secNum = [secondStr integerValue];
    // 3. 计算
    NSInteger sum = firNum + secNum;
    // 4. int转string并输出
    NSString *Sum = [NSString stringWithFormat:@"%ld",(long)sum];
    self.sumTextField.text = Sum;
}

提示框代码(iOS9过后使用UIAlertView会有过期的警告,不过仍旧可以用)

- (void) ShowInfo: (NSString *)info{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"输入有误" message:info delegate:nil cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];
    [alertView show];
}
控件概述
UIButton - 按钮
  • 按钮的作用是:监听用户的点击事件,在用户点击后做出响应。
UITextField - 文本输入框
  • 文本输入框可以弹出键盘,让用户输入文本内容。
UITextView - 能滚动的文字显示控件
  • 如果文字内容比较多,需要换行显示,并且需要编辑。
UIProgressView - 进度条
  • 水平进度条,比如显示文件的下载进度,程序的启动进度。
UISlider - 滑块
  • 在两个数值之间滑动选择,比如调节音量大小。
UIActivityIndicator - 圈圈
  • 一直在转圈,表示让用户等待。
UIAlertView - 对话框(中间弹框)
  • 从中间弹出一个框提示用户下一步该做什么。
UIActionSheet - 底部弹框
  • 从底部弹出一个框提示用户下一步该做什么。
UIScrollView - 滚动的控件
  • 如果内容比较多,超出了一个屏幕,就可以用它来显示。
UIPageControl - 分页控件
  • 能显示当前页码。
UITableView - 表格
  • 如果每一行显示的内容格式查不多,就用这个表格控件。
UICollectionView - 九宫格
  • 如果显示的东西是一块一块、一格一格的,而且每个格子长的差不多,就可以用它。
UIWebView - 网页显示控件
  • 一般用来显示网页,使用它,就可以在手机上浏览网页。
UISwitch - 开关
  • 要么打开,要么关上。
UISegmentControl - 选项卡
  • 在固定的几个选项之间进行选择。
UIPickerView - 选择器
  • 在多行数据之间只选择一行。
UIDatePicker - 日期选择器
  • 选择日期。
UIToolbar - 工具条
  • 一般显示在底部或者键盘顶部,里面有几个小按钮。
UINavigationBar - 导航条
  • 显示在顶部的条
重要控件详述
UILabel的常见属性
  • @property(nullable, nonatomic,copy) NSString *text;
    显示的文字
  • @property(null_resettable, nonatomic,strong) UIFont *font;
    字体
  • @property(null_resettable, nonatomic,strong) UIColor *textColor;
    文字颜色
  • @property(nonatomic) NSTextAlignment textAlignment;
    对齐模式(比如左对齐、居中对齐、右对齐)
  • @property(nonatomic) NSInteger numberOfLines;
    文字行数
  • @property(nonatomic) NSLineBreakMode lineBreakMode;
    换行模式
    相关代码如下
//1.1 创建UILabel对象
    UILabel * label = [[UILabel alloc] init];
    //1.2 设置frame
    label.frame = CGRectMake(100, 100, 200, 150);
    //1.3 设置背景颜色
    label.backgroundColor = [UIColor purpleColor];
    //1.4 设置文字
    label.text = @"湖人总冠军! Lakers Championship!! Huren ZongGuanJun!!!";
    //1.5 设置文字颜色
    label.textColor = [UIColor yellowColor];
    //1.6 设置字体大小
    label.font = [UIFont systemFontOfSize:20.f];
    label.font = [UIFont boldSystemFontOfSize:25.f];
    label.font = [UIFont italicSystemFontOfSize:30.f];
    //1.7 设置文字阴影(默认有值)
    label.shadowColor = [UIColor blackColor];
    label.shadowOffset = CGSizeMake(-2, 1);
    //1.8 居中
    label.textAlignment = NSTextAlignmentCenter;
    //1.9 显示模式
    label.lineBreakMode = NSLineBreakByTruncatingTail;
    label.numberOfLines = 0;
    //2.0 添加到控制器的view中

效果图如图
在这里插入图片描述

UIImageView的常见属性
  • @property (nullable, nonatomic, strong) UIImage *image;
    显示的图片
  • @property (nullable, nonatomic, copy) NSArray<UIImage *> *animationImages;
    显示的动画图片
  • @property (nonatomic) NSTimeInterval animationDuration;
    动画图片的持续时间
  • @property (nonatomic) NSInteger animationRepeatCount;
    动画的播放次数(默认是0,代表无限播放)
设置frame的方式

方式一

    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:@"1"];
    imageView.frame = CGRectMake(100, 100, 200, 200);
    
    [self.view addSubview:imageView];

方式二

UIImageView *imageView = [[UIImageView alloc] init];
    UIImage *image = [UIImage imageNamed:@"1"];
    imageView.frame = CGRectMake(100, 10, image.size.width, image.size.height);
    imageView.image = image;
    
    [self.view addSubview:imageView];

方式三

 UIImage *image = [UIImage imageNamed:@"1"];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 10, image.size.width, image.size.height)];
    imageView.image = image;
    
    [self.view addSubview:imageView];

方式四

//默认尺寸是图片尺寸
    //默认尺寸是图片尺寸
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1"]];
    //改变位置
    imageView.center = CGPointMake(self.view.frame.size.width, self.view.frame.size.height);
    
    [self.view addSubview:imageView];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值