ios教程(1)--加法計數器

來源:互聯網
上載者:User

標籤:ios

大家好因為我第一次寫ios的教程,寫的不好請大家對對原諒 (噠噠:廢話真多)

今天的重點有幾點

1、鍵盤的退出 鍵盤的樣式

2、手動建立代碼

3、CGRectGetMaxX() 擷取最大的X值 參數(傳一個空間的frame進去)

(噠噠:覺得好難,吃不下怎麼辦)


////  ViewController.m//  01-加法計算機(代碼實現)////  Created by sunda on 15/6/29.//  Copyright (c) 2015年 sunda. All rights reserved.//#import "ViewController.h"#define kPaddiing 20@interface ViewController ()//結果@property (nonatomic,strong) UILabel *ResultLable;//計算按鈕@property (nonatomic,strong) UIButton *CalculationButton;//第一個文字框@property (nonatomic,strong) UITextField *name1;//第二個文字框@property (nonatomic,strong) UITextField *name2;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    //=================建立第一個文字框=============//    //1、建立文字框    self.name1 = [[UITextField alloc] init];    //設定位置    self.name1.frame = CGRectMake(20, 20, 80, 30);    //UITextBorderStyle    self.name1.borderStyle = UITextBorderStyleRoundedRect;    //設定彈出鍵盤的樣式    self.name1.keyboardType = UIKeyboardTypeNumberPad;    [self.view addSubview:self.name1];        //=================建立 + lable=================//    UILabel *numbel1 = [[UILabel alloc] init];    //擷取name1的最大的X    CGFloat numbel1X = CGRectGetMaxX(self.name1.frame) + kPaddiing;    numbel1.frame = CGRectMake(numbel1X, 20, 10, 30) ;    numbel1.text = @"+";    [self.view addSubview:numbel1];        //=================建立第二個文字框=============//    self.name2 = [[UITextField alloc] init];    CGFloat name2X = CGRectGetMaxX(numbel1.frame) + kPaddiing;    //設定位置    self.name2.frame = CGRectMake(name2X, 20, 80, 30);    //UITextBorderStyle    self.name2.borderStyle = UITextBorderStyleRoundedRect;    //設定彈出鍵盤的樣式    self.name1.keyboardType = UIKeyboardTypeNumberPad;    [self.view addSubview:self.name2];    //=================建立 = lable=================//    UILabel *numbel2 = [[UILabel alloc] init];    //擷取name1的最大的X    CGFloat numbel2X = CGRectGetMaxX(self.name2.frame) + kPaddiing;    numbel2.frame = CGRectMake(numbel2X, 20, 10, 30) ;    numbel2.text = @"=";    [self.view addSubview:numbel2];        //================建立 結果 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];        //===============建立計算結果按鈕==============//    self.CalculationButton = [[UIButton alloc] init];    self.CalculationButton.frame = CGRectMake(150, 100, 50, 50);    [self.CalculationButton setTitle:@"計算" forState:UIControlStateNormal];    [self.CalculationButton setBackgroundColor:[UIColor redColor]];    [self.view addSubview:self.CalculationButton];    //添加點擊事件    [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];    //關閉鍵盤    [self.view endEditing:YES];}@end
在這裡 說一下點擊事件,又叫監聽方法 (嘿嘿但是我比較喜歡叫他點擊事件)所以看個人

關於所以點擊事件一些提示提示:

1> addTarget方法定義在UIControl類中,這意味著可以給所有繼承自UIControl類的對象添加監聽方法

2> 監聽方法的第一個參數就是對象本身

3> 監聽方法的第二個參數是監聽控制項的事件


好了,第一個小的dome就說到這裡,哪裡錯誤了請大家幫忙說出來

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

ios教程(1)--加法計數器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.