zoukankan      html  css  js  c++  java
  • UI —— 计算器

    #import <UIKit/UIKit.h>


    @interface MyViewController :UIViewController

    {

       NSInteger _firstName;

       NSInteger _calculator; // 数字的计数器

       NSInteger _buttonType; // 计算符号计数器

    }


    @property (nonatomic,retain) UITextField *inputField;


    @end







    #import "MyViewController.h"


    @interface MyViewController ()


    @end


    @implementation MyViewController


    -(void)dealloc

    {

        [_inputField release];

        

        [superdealloc];

    }


    - (void)viewDidLoad {

        [superviewDidLoad];

        // Do any additional setup after loading the view.

        

        //计算器的1 - 10个数字键

       CGRect rect = CGRectMake(40,0, 50, 30);

       for (int i =0; i < 10; i++) {

            rect.origin.y +=50;

           NSString *s = [NSStringstringWithFormat:@"%d", i];

            UIButton *numButton = [UIButtonbuttonWithType:UIButtonTypeCustom];

            [numButtonsetFrame:rect];

            [numButton setBackgroundColor:[UIColorcyanColor]];

            [numButton setTitle:sforState:UIControlStateNormal];

            [self.viewaddSubview:numButton];

            

            [numButton addTarget:selfaction:@selector(buttonClick:)forControlEvents:UIControlEventTouchUpInside];

        }

        

       CGRect textRect = CGRectMake(130, 50, 80, 30);

       _inputField = [[UITextFieldalloc] initWithFrame:textRect];

        [_inputFieldsetKeyboardType:UIKeyboardTypeNamePhonePad];

        [_inputFieldsetBackgroundColor:[UIColorgrayColor]];

        [self.viewaddSubview:_inputField];

        [_inputField release];

        

        textRect.size.width =50;

        textRect.origin.y +=50;

        UIButton *addButton = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

        [addButtonsetFrame:textRect];

        [addButton setBackgroundColor:[UIColorbrownColor]];

        [addButton setTitle:@"="forState:UIControlStateNormal];

        [self.viewaddSubview:addButton];

        

        [addButton addTarget:selfaction:@selector(buttonEqual:)forControlEvents:UIControlEventTouchUpInside];

        

       for (int j =0; j < 4; j++) {

           NSArray *symbolArray = [NSArrayarrayWithObjects:@"+",@"-", @"*",@"/", nil];

           NSString *symbolString = [symbolArray objectAtIndex:j];

            textRect.origin.y +=50;

            UIButton *symbolButton = [UIButtonbuttonWithType:UIButtonTypeCustom];

            [symbolButtonsetFrame:textRect];

            [symbolButtonsetBackgroundColor:[UIColorgreenColor]];

            [symbolButtonsetTitle:symbolString forState:UIControlStateNormal];

            [self.viewaddSubview:symbolButton];

            [symbolButtonsetTag:j];

            

            [symbolButton addTarget:selfaction:@selector(calculateSymbolButton:)forControlEvents:UIControlEventTouchUpInside];

        }


        textRect.origin.y +=50;

        UIButton *clearButton = [UIButtonbuttonWithType:UIButtonTypeCustom];

        [clearButton setBackgroundColor:[UIColorpurpleColor]];

        [clearButton setTitle:@"C"forState:UIControlStateNormal];

        [clearButtonsetFrame:textRect];

        [self.viewaddSubview:clearButton];

        

        [clearButton addTarget:selfaction:@selector(clearButton:)forControlEvents:UIControlEventTouchUpInside];

    }


    - (void)buttonClick:(UIButton *)sender

    {

       NSString *inputString = nil;

       if (0 == _calculator) {

            inputString = [_inputField.textstringByAppendingString:sender.titleLabel.text];

        }else{

            inputString = sender.titleLabel.text;

        }

        _calculator = 0;

        [_inputFieldsetText:inputString];

    }


    - (void)calculateSymbolButton:(UIButton *)sender

    {

        //此处另一点小瑕疵,须要在推断一下

        [selfbuttonEqual:nil];

        _firstName = [_inputField.textintegerValue];

        //保存输入的下一个数的形式

        _calculator = 1;

        // 保存计算类型 + - / *(tag更简单一点)

       _buttonType = sender.tag;

    }


    - (void)clearButton:(UIButton *)sender

    {

        _firstName = 0;

        _calculator = 0;

        _buttonType = 0;

        _inputField.text =@"";

    }


    - (void)buttonEqual:(UIButton *)sender

    {

       NSInteger result;

        switch (_buttonType) {

           case 0:

                result =_firstName + [_inputField.textintegerValue];

               break;

                

           case 1:

                result =_firstName - [_inputField.textintegerValue];

               break;

                

           case 2:

                result =_firstName * [_inputField.textintegerValue];

               break;

                

           case 3:

                result =_firstName / [_inputField.textintegerValue];

               break;

                

           default:

               break;

        }

        

       NSString *end = [NSStringstringWithFormat:@"%ld", result];

       _inputField.text = end;

        

    }



    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    opencv 5 图像转换(1 边缘检测)
    opencv 4 图像处理(漫水填充,图像金字塔与图片尺寸缩放,阈(yu)值化)
    opencv 4 图像处理(2 形态学滤波:腐蚀与膨胀,开运算、闭运算、形态学梯度、顶帽、黑帽)
    HDU 1847-Good Luck in CET-4 Everybody!-博弈SG函数模板
    网络流
    multiset的erase()操作中出现跳过元素的问题
    HRBUST
    L1-8 矩阵A乘以B (15 分)
    L2-2 重排链表 (25 分)
    L2-4 部落 (25 分)
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4817285.html
Copyright © 2011-2022 走看看