Design of the calculator using UI custom controls in OC (version 1 simple subtraction, add, subtract, connect, connect)

Source: Internet
Author: User

Design of the calculator using UI custom controls in OC (version 1 simple subtraction, add, subtract, connect, connect)
#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 V    Iewdidload];    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: (uibut    ton *) 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 the can be recreated.} @end


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Design of the calculator using UI custom controls in OC (version 1 simple subtraction, add, subtract, connect, connect)

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.