IOS控件:长度单位换算(UITextField,IBAction,委托事件touchesBegan)

#import <UIKit/UIKit.h>

@interface demo7_dayViewController : UIViewController {
    IBOutlet UITextField *chi;     //
    IBOutlet UITextField *meter;   //
    IBOutlet UITextField *feet;    // 英尺
}

// 此处 对每个文本框声明程序属性
@property (nonatomic, retain) UITextField *chi;
@property (nonatomic, retain) UITextField *meter;
@property (nonatomic, retain) UITextField *feet;

-(IBAction)reset;

@end

 

#import "demo7_dayViewController.h"

@interface demo7_dayViewController ()

@end

@implementation demo7_dayViewController

// 本指令告诉编译器去合成所需的“文本框”控件的存储器方法
@synthesize chi, feet, meter;

// 构建重置文本框的长度单位数值的按钮方法
-(IBAction)reset {
    chi.text = @"3.0003";   //
    meter.text = @"1";      //
    feet.text = @"3.2808";  // 英尺
}

// 捕获手指的触摸事件方法,触摸开始时会调用touchesBegan
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    // 建立视图触摸“touch”对象定制所有事件中的任何触摸对象
    UITouch *touch = [[event allTouches] anyObject];
    // 当手指在屏幕的触摸次数>=1次
    if (touch.tapCount > 1) {
        // 隐藏文本框输入时的键盘
        [chi resignFirstResponder];
        [meter resignFirstResponder];
        [feet resignFirstResponder];
    }
}

// 当点击return返回键时,键盘自动隐藏,返回单位转换计算结果
-(BOOL)textFieldShouldReturn:(UITextField*)textField {
    if (textField == chi) {
        [chi resignFirstResponder];
        // 建立浮点变量,计算后转换成“米”
        double m = [chi.text doubleValue]/3.003;
        // 建立浮点变量,计算后转换成“英尺”
        double f = m * 3.2808;
        
        // 把字符类型字符按固定格式定制给对象,"%.4f"的位置将显示小数点后4位浮点变量。
        meter.text = [[NSString alloc]initWithFormat:@"%.4f", m];
        feet.text = [[NSString alloc]initWithFormat:@"%.4f", f];
        
    }
    if (textField == meter) {
        [meter resignFirstResponder];
        double c = [meter.text doubleValue] * 3.003;
        double f = [meter.text doubleValue] * 3.2808;
        chi.text = [[NSString alloc]initWithFormat:@"%.4f", c];
        feet.text = [[NSString alloc]initWithFormat:@"%.4f", f];
    }
    if (textField == feet) {
        [feet resignFirstResponder];
        double m = [feet.text doubleValue]/3.2808;
        double c = m * 3.003;
        meter.text = [[NSString alloc]initWithFormat:@"%.4f", m];
        chi.text = [[NSString alloc]initWithFormat:@"%.4f", c];
    }
    
    return YES;
}

//- (void)viewDidLoad
//{
//    [super viewDidLoad];
//    // Do any additional setup after loading the view, typically from a nib.
//}

// 释放可用内存供给应用程序,并提早发出警告提示
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// 执行内存清理工作
-(void)dealloc{
    [chi release];
    [meter release];
    [feet release];
    [super dealloc];
}

@end

 

 

posted on 2013-08-24 15:26  大米稀饭  阅读(615)  评论(0编辑  收藏  举报