您的位置:首页 > 编程语言

纯代码 实现简易计算器界面

2020-06-10 04:23 525 查看

//

//  ViewController.m

//  01-calculate

//

//  Created by 王 on 16/4/9.

//  Copyright © 2016年 王. All rights reserved.

//

 

#import "ViewController.h"

 

@interface ViewController ()

//设置全局变量 方面下面计算方法实现时使用

@property (nonatomic,weak)UITextField *num1Text;

@property (nonatomic,weak)UITextField *num2Text;

@property (nonatomic,weak)UILabel *num3Label;

@end

 

@implementation ViewController

 

//视图加载后会自动执行

- (void)viewDidLoad {

    [super viewDidLoad];

    [self setupUi];

    // Do any additional setup after loading the view, typically from a nib.

}

 

#pragma mark - 分界线

//设置界面

- (void)setupUi{

    

    //text框

    UITextField *myText1 = [[UITextField alloc]init];

    UITextField *myText2 = [[UITextField alloc]init];

    //更改text框的背景色

    myText1.backgroundColor = [UIColor redColor];

    myText2.backgroundColor = [UIColor greenColor];

    //下面也同样可以实现,比较繁琐

//    CGRect rect = myText1.frame;

//    rect.origin.x = 10;

//    rect.origin.y = 20;

//    rect.size.height = 20;

//    rect.size.width = 40;

//    myText.frame = rect;

    

    //设置text框的大小

    myText1.frame = CGRectMake(10, 20, 50, 20);

    myText2.frame = CGRectMake(90, 20, 50, 20);

    //设置text1对应的纯数字键盘

    myText1.keyboardType=UIKeyboardTypeNumberPad;

    //添加到根视图

    [self.view addSubview:myText1];

    [self.view addSubview:myText2];

    

    //赋值

    _num1Text = myText1;

    _num2Text = myText2;

    

    //创建Label

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

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

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

    //改变Label的大小

    myLabel1.frame = CGRectMake(70, 20, 7, 10);

    myLabel2.frame = CGRectMake(150, 20, 7, 10);

    myLabel3.frame = CGRectMake(165, 20, 7, 10);

    //更改Label的text内容

    myLabel1.text = @"+";

    myLabel2.text = @"=";

    myLabel3.text = @"0";

    //自动根据内容调整Label的大小

    [myLabel1 sizeToFit];

    [myLabel2 sizeToFit];

    [myLabel3 sizeToFit];

    //添加到根视图

    [self.view addSubview:myLabel1];

    [self.view addSubview:myLabel2];

    [self.view addSubview:myLabel3];

    

    //赋给全局变量

    _num3Label = myLabel3;

    

    //Botton按钮

    UIButton *myBotton = [[UIButton alloc]initWithFrame:CGRectMake(20, 40, 30, 30)];

    //给button添加计算二字

    [myBotton setTitle:@"计算" forState:UIControlStateNormal];

    //默认状态 "计算"的颜色

    [myBotton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];

    //高亮状态 "计算"的颜色

    [myBotton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

    //自动调整button的大小

    [myBotton sizeToFit];

    //添加到根视图

    [self.view addSubview:myBotton];

    

    //实现点击事件

    [myBotton addTarget:self action:@selector(calculate) forControlEvents:UIControlEventTouchUpInside];

}

 

//计算方法实现

- (void)calculate{

    NSInteger num1 = _num1Text.text.intValue;

    NSInteger num2 = _num2Text.text.intValue;

    //结果

    NSInteger result = num1+num2;

    //float类型结果转化成string类型

    _num3Label.text = @(result).description;

    //自动调整结果框大小

    [_num3Label sizeToFit];

}

 

 

 

@end

 

转载于:https://www.cnblogs.com/bywjb/p/5376763.html

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: