//
Viewcontroller.m
01-calculate
//
Created by Wang on 16/4/9.
COPYRIGHT©2016 King of the year. All rights reserved.
//
#import "ViewController.h"
@interface Viewcontroller ()
Setting global variables is used when the following calculation method is implemented
@property (nonatomic,weak) Uitextfield *num1text;
@property (nonatomic,weak) Uitextfield *num2text;
@property (nonatomic,weak) UILabel *num3label;
@end
@implementation Viewcontroller
The view is automatically executed after it is loaded
-(void) Viewdidload {
[Super Viewdidload];
[Self setupui];
Additional setup after loading the view, typically from a nib.
}
#pragma mark-the dividing line
Setup interface
-(void) setupui{
text box
Uitextfield *mytext1 = [[Uitextfield alloc]init];
Uitextfield *mytext2 = [[Uitextfield alloc]init];
Change the background color of the text box
Mytext1.backgroundcolor = [Uicolor Redcolor];
Mytext2.backgroundcolor = [Uicolor Greencolor];
The following can also be achieved, more cumbersome
CGRect rect = mytext1.frame;
rect.origin.x = 10;
RECT.ORIGIN.Y = 20;
Rect.size.height = 20;
Rect.size.width = 40;
Mytext.frame = rect;
Set the size of the text box
Mytext1.frame = CGRectMake (10, 20, 50, 20);
Mytext2.frame = CGRectMake (90, 20, 50, 20);
Set up a Text1-compatible keypad
Mytext1.keyboardtype=uikeyboardtypenumberpad;
Add to Root view
[Self.view Addsubview:mytext1];
[Self.view ADDSUBVIEW:MYTEXT2];
Assign value
_num1text = MyText1;
_num2text = myText2;
Create a label
UILabel *mylabel1 = [[UILabel alloc]init];
UILabel *mylabel2 = [[UILabel alloc]init];
UILabel *mylabel3 = [[UILabel alloc]init];
Change the size of a label
Mylabel1.frame = CGRectMake (70, 20, 7, 10);
Mylabel2.frame = CGRectMake (150, 20, 7, 10);
Mylabel3.frame = CGRectMake (165, 20, 7, 10);
Change the text content of a label
Mylabel1.text = @ "+";
Mylabel2.text = @ "=";
Mylabel3.text = @ "0";
Automatically resizes the label based on content
[MyLabel1 SizeToFit];
[MyLabel2 SizeToFit];
[MyLabel3 SizeToFit];
Add to Root view
[Self.view Addsubview:mylabel1];
[Self.view Addsubview:mylabel2];
[Self.view Addsubview:mylabel3];
assigning to global variables
_num3label = MyLabel3;
Botton button
UIButton *mybotton = [[UIButton alloc]initwithframe:cgrectmake (20, 40, 30, 30)];
Add a calculation Word to the button
[Mybotton settitle:@ "calculation" forstate:uicontrolstatenormal];
Color of the default state "calculation"
[Mybotton Settitlecolor:[uicolor Redcolor] forstate:uicontrolstatehighlighted];
Color of the "calculated" highlight state
[Mybotton Settitlecolor:[uicolor Bluecolor] forstate:uicontrolstatenormal];
Automatically resize the button
[Mybotton SizeToFit];
Add to Root view
[Self.view Addsubview:mybotton];
Implement Click events
[Mybotton addtarget:self Action: @selector (calculate) forcontrolevents:uicontroleventtouchupinside];
}
Calculation method implementation
-(void) calculate{
Nsinteger num1 = _num1text.text.intvalue;
Nsinteger num2 = _num2text.text.intvalue;
Results
Nsinteger result = num1+num2;
Float type result converted to String type
_num3label.text = @ (result). Description;
AutoFit Result Box size
[_num3label SizeToFit];
}
@end
Pure Code Implementation Simple Calculator interface