In OC, the User-Defined UI control is used to implement the calculator design (version 1 is simple addition, subtraction, multiplication, addition, subtraction, division, and multiplication), ocui

Source: Internet
Author: User

In OC, the User-Defined UI control is used to implement the calculator design (version 1 is simple addition, subtraction, multiplication, addition, subtraction, division, and multiplication), ocui

In OC, the UI custom control is used to implement the calculator design (version 1 is simple addition, subtraction, multiplication, and division, addition, subtraction, division, and multiplication)
#import <UIKit/UIKit.h>@interface ViewController : UIViewController<UIApplicationDelegate>{    float _operator1;    float _operator2;    float _result;}@end
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    UITextField * textFied1 = [[UITextField alloc]initWithFrame:CGRectMake(10, 30, 300 ,50)];    textFied1.borderStyle = UITextBorderStyleLine;    textFied1.backgroundColor = [UIColor redColor];    [self.view addSubview:textFied1];    textFied1.clearButtonMode = UITextFieldViewModeAlways;    textFied1.tag = 007;                UIButton * btn1 = [UIButton buttonWithType:UIButtonTypeCustom];    btn1.Frame=CGRectMake(10, 90, 70, 70);    btn1.backgroundColor = [UIColor orangeColor];    [self.view addSubview:btn1];    [btn1 setTitle:@"7" forState:UIControlStateNormal];        [btn1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];    btn1.tag = 7;          UIButton * btn2 = [UIButton buttonWithType:UIButtonTypeCustom];    btn2.Frame=CGRectMake(85, 90, 70, 70);    btn2.backgroundColor = [UIColor orangeColor];    [self.view addSubview:btn2];    [btn2 setTitle:@"8" forState:UIControlStateNormal];    btn2.tag = 8;        [btn2 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];            UIButton * btn3 = [UIButton buttonWithType:UIButtonTypeCustom];    btn3.Frame=CGRectMake(160, 90, 70, 70);    btn3.backgroundColor = [UIColor orangeColor];    [self.view addSubview:btn3];    [btn3 setTitle:@"9" forState:UIControlStateNormal];    btn3.tag = 9;       [btn3 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];        UIButton * btn4 = [UIButton buttonWithType:UIButtonTypeCustom];    btn4.Frame=CGRectMake(235, 90, 70, 70);    btn4.backgroundColor = [UIColor orangeColor];    [self.view addSubview:btn4];    [btn4 setTitle:@"/" forState:UIControlStateNormal];    btn4.tag = 13;       [btn4 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];        UIButton * btn5 = [UIButton buttonWithType:UIButtonTypeCustom];    btn5.Frame=CGRectMake(10, 165, 70, 70);    btn5.backgroundColor = [UIColor orangeColor];    [self.view addSubview:btn5];    [btn5 setTitle:@"4" forState:UIControlStateNormal];    btn5.tag = 4;        [btn5 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];        UIButton * btn6 = [UIButton buttonWithType:UIButtonTypeCustom];    btn6.Frame=CGRectMake(85, 165, 70, 70);    btn6.backgroundColor = [UIColor orangeColor];    [self.view addSubview:btn6];    [btn6 setTitle:@"5" forState:UIControlStateNormal];    btn6.tag = 5;    [btn6 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];            UIButton * btn7 = [UIButton buttonWithType:UIButtonTypeCustom];    btn7.Frame=CGRectMake(160, 165, 70, 70);    btn7.backgroundColor = [UIColor orangeColor];    [self.view addSubview:btn7];     [btn7 setTitle:@"6" forState:UIControlStateNormal];    btn7.tag = 6;   [btn7 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];            UIButton * btn8 = [UIButton buttonWithType:UIButtonTypeCustom];    btn8.Frame=CGRectMake(235, 165, 70, 70);    btn8.backgroundColor = [UIColor orangeColor];    [self.view addSubview:btn8];     [btn8 setTitle:@"X" forState:UIControlStateNormal];    btn8.tag = 12;    [btn8 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];        UIButton * btn9 = [UIButton buttonWithType:UIButtonTypeCustom];    btn9.Frame=CGRectMake(10, 240, 70, 70);    btn9.backgroundColor = [UIColor orangeColor];    [self.view addSubview:btn9];    [btn9 setTitle:@"1" forState:UIControlStateNormal];    btn9.tag = 1;     [btn9 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];        UIButton * btn10 = [UIButton buttonWithType:UIButtonTypeCustom];    btn10.Frame=CGRectMake(85, 240, 70, 70);    btn10.backgroundColor = [UIColor orangeColor];    [self.view addSubview:btn10];     [btn10 setTitle:@"2" forState:UIControlStateNormal];    btn10.tag = 2;    [btn10 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];            UIButton * btn11 = [UIButton buttonWithType:UIButtonTypeCustom];    btn11.Frame=CGRectMake(160, 240, 70, 70);    btn11.backgroundColor = [UIColor orangeColor];    [self.view addSubview:btn11];     [btn11 setTitle:@"3" forState:UIControlStateNormal];    btn11.tag = 3;   [btn11 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];            UIButton * btn12 = [UIButton buttonWithType:UIButtonTypeCustom];    btn12.Frame=CGRectMake(235, 240, 70, 70);    btn12.backgroundColor = [UIColor orangeColor];    [self.view addSubview:btn12];     [btn12 setTitle:@"-" forState:UIControlStateNormal];    btn12.tag = 11;   [btn12 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];        UIButton * btn13 = [UIButton buttonWithType:UIButtonTypeCustom];    btn13.Frame=CGRectMake(10, 315, 70, 70);    btn13.backgroundColor = [UIColor orangeColor];    [self.view addSubview:btn13];     [btn13 setTitle:@"0" forState:UIControlStateNormal];    btn13.tag = 0;   [btn13 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];        UIButton * btn14 = [UIButton buttonWithType:UIButtonTypeCustom];    btn14.Frame=CGRectMake(85, 315, 145, 70);    btn14.backgroundColor = [UIColor orangeColor];    [self.view addSubview:btn14];     [btn14 setTitle:@"=" forState:UIControlStateNormal];    btn14.tag = 14;   [btn14 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];        UIButton * btn15 = [UIButton buttonWithType:UIButtonTypeCustom];    btn15.Frame=CGRectMake(235, 315, 70, 70);    btn15.backgroundColor = [UIColor orangeColor];    [self.view addSubview:btn15];    [btn15 setTitle:@"+" forState:UIControlStateNormal];    btn15.tag = 10;    [btn15 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];        // Do any additional setup after loading the view, typically from a nib.}//enum calCul//{//    add,//    sub,//    mul,//    mod//};////typedef enum calCul calCul;int count;-(void)btnClick:(UIButton *)btn{    NSString * str = [[NSString alloc]init];    if (btn.tag == 0) {        UITextField * field = ( UITextField * )[self.view viewWithTag:007];        str = @"0";        //_operator1 = 0;                field.text = str;    }    else if(btn.tag==1)    {        UITextField * field = ( UITextField * )[self.view viewWithTag:007];        str = @"1";        //_operator2 = 1;        field.text = str;    }    else if(btn.tag==2)    {        UITextField * field = ( UITextField * )[self.view viewWithTag:007];        str = @"2";        field.text = str;    }    else if(btn.tag==3)    {        UITextField * field = ( UITextField * )[self.view viewWithTag:007];        str = @"3";        field.text = str;    }    else if(btn.tag==4)    {        UITextField * field = ( UITextField * )[self.view viewWithTag:007];        str = @"4";        field.text = str;    }    else if(btn.tag==5)    {        UITextField * field = ( UITextField * )[self.view viewWithTag:007];        str = @"5";        field.text = str;    }    else if(btn.tag==6)    {        UITextField * field = ( UITextField * )[self.view viewWithTag:007];        str = @"6";        field.text = str;    }    else if(btn.tag==7)    {        UITextField * field = ( UITextField * )[self.view viewWithTag:007];        str = @"7";        field.text = str;    }    else if(btn.tag==8)    {        UITextField * field = ( UITextField * )[self.view viewWithTag:007];        str = @"8";        field.text = str;    }    else if(btn.tag==9)    {        UITextField * field = ( UITextField * )[self.view viewWithTag:007];        str = @"9";        field.text = str;    }    else if(btn.tag==10)    {        count = 1;        UITextField * field = ( UITextField * )[self.view viewWithTag:007];        //str = @"+";        str = field.text;        //_result = _operator1;        _operator1 = [str doubleValue];                //str = [[NSString alloc]initWithFormat:@"%.2f",_result];        field.text = str;    }    else if(btn.tag==11)    {        count = 2;        UITextField * field = ( UITextField * )[self.view viewWithTag:007];        //str = @"-";        //_result = _operator1 + _operator2;        str = field.text;        _operator1 = [str doubleValue];        field.text = str;    }    else if(btn.tag==12)    {        count = 3;        UITextField * field = ( UITextField * )[self.view viewWithTag:007];        //str = @"*";        str = field.text;        _operator1 = [str doubleValue];        field.text = str;    }    else if(btn.tag==13)    {        count = 4;        UITextField * field = ( UITextField * )[self.view viewWithTag:007];        //str = @"/";        str = field.text;        _operator1 = [str doubleValue];        field.text = str;    }    else if(btn.tag==14)    {        UITextField * field = ( UITextField * )[self.view viewWithTag:007];        str = @"=";        str = field.text;        _operator2 = [str doubleValue];        if (count==1) {            _result = _operator1 + _operator2;        }        else if(count == 2)        {        _result = _operator1 - _operator2;        }        else if(count == 3)        {        _result = _operator1 * _operator2;        }        else if(count==4)        {        _result = _operator1 / _operator2;        }        str = [[NSString alloc]initWithFormat:@"%.6f",_result];        field.text = str;    }    }-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{    return YES;}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.