Hello everyone because I first write iOS tutorial, write bad please everyone to forgive ( da da: Nonsense really much )
There are a few points today.
1. Keyboard exit keyboard style
2. Create code manually
3, Cgrectgetmaxx () get the maximum X-value parameter (pass a space frame inside)
( da da: Feel very difficult, can't eat how to do )
viewcontroller.m//01-Addition Calculator (code implementation)////Created by Sunda on 15/6/29.//Copyright (c) 2015 Sunda. All rights reserved.//#import "ViewController.h" #define kpaddiing 20@interface Viewcontroller ()//Results @property ( Nonatomic,strong) UILabel *resultlable;//calculation button @property (nonatomic,strong) UIButton *calculationbutton;// First text box @property (nonatomic,strong) Uitextfield *name1;//second text box @property (nonatomic,strong) Uitextfield *name2;@ End@implementation viewcontroller-(void) viewdidload {[Super viewdidload]; ================= Create the first text box =============////1, create a text box SELF.NAME1 = [[Uitextfield alloc] init]; Set Position Self.name1.frame = CGRectMake (20, 20, 80, 30); Uitextborderstyle Self.name1.borderStyle = Uitextborderstyleroundedrect; Sets the style of the pop-up keyboard self.name1.keyboardType = Uikeyboardtypenumberpad; [Self.view addSubview:self.name1]; ================= Create + lable=================//UILabel *numbel1 = [[UILabel alloc] init]; Gets the maximum x cgfloat of name1 numbel1x = Cgrectgetmaxx (self.name1.frame) + kpaddiing; Numbel1.frame = CGRectMake (numbel1x, 20, 10, 30); Numbel1.text = @ "+"; [Self.view Addsubview:numbel1]; ================= Create a second text box =============//self.name2 = [[Uitextfield alloc] init]; CGFloat name2x = Cgrectgetmaxx (numbel1.frame) + kpaddiing; Set Position self.name2.frame = CGRectMake (name2x, 20, 80, 30); Uitextborderstyle Self.name2.borderStyle = Uitextborderstyleroundedrect; Sets the style of the pop-up keyboard self.name1.keyboardType = Uikeyboardtypenumberpad; [Self.view addSubview:self.name2]; ================= Create = lable=================//UILabel *numbel2 = [[UILabel alloc] init]; Gets the largest x cgfloat numbel2x = Cgrectgetmaxx (self.name2.frame) + kpaddiing of the name1; Numbel2.frame = CGRectMake (numbel2x, 20, 10, 30); Numbel2.text = @ "="; [Self.view Addsubview:numbel2]; ================ creates the result lable==============//self. resultlable = [[UILabel alloc] init]; CGFloat Resultlablex = CgrectgetmAxX (numbel2.frame) + kpaddiing; Self. Resultlable.frame = CGRectMake (Resultlablex, 20, 80, 30); Self. Resultlable.text = @ "0"; [Self.view addsubview:self. Resultlable]; =============== Create a calculated result button ==============//self. Calculationbutton = [[UIButton alloc] init]; Self. Calculationbutton.frame = CGRectMake (150, 100, 50, 50); [Self. Calculationbutton settitle:@ "calculation" forstate:uicontrolstatenormal]; [Self. Calculationbutton Setbackgroundcolor:[uicolor Redcolor]]; [Self.view addsubview:self. Calculationbutton]; Add Click event [Self. Calculationbutton addtarget:self Action: @selector (calculation) forcontrolevents:uicontroleventtouchupinside];} -(void) calculation{NSLog (@ "%@------%@", self.name1.text,self.name2.text); float NUM1 = Self.name1.text.integerValue; float num2 = Self.name2.text.integerValue; Self. Resultlable.text = [NSString stringwithformat:@ "%.2f", num1+num2]; Close the keyboard [Self.view Endediting:yes];} @end
Here is the Click event, also known as the monitoring method (hey, but I prefer to call him click events) so look at the individual
For some tips on clicking events:
1 > AddTarget method defines the Uicontrol class, which means that you can give all inherited from the Uicontrol class to add a listener method to an object
2 > the first parameter of a listening method is the object itself
3 > the second parameter of the listener method is the event that listens to the control
Well, the first little dome is here, where is wrong, please help me out.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
iOS Tutorials (1)--Addition counters