将UITextField的内容设置到UILable上,求两个UITextField的和到UILable上

//
//  CCLAppDelegate.m
//  TestButton
//
//  Created by lanouhn on 14-8-19.
//  Copyright (c) 2014年 vaercly@163.com 陈聪雷. All rights reserved.
//

#import "CCLAppDelegate.h"

@interface CCLAppDelegate ()
{
    UIView *_bgView;
}
@end

@implementation CCLAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    //创建背景视图
    [self creatbgView];
    //将textField的内容显示在lable中
    [self lableToTextField];
    //加法
    [self addMethod];
    [self.window makeKeyAndVisible];
    return YES;
}

//创建背景视图
- (void)creatbgView
{
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
    _bgView.backgroundColor = [UIColor whiteColor];
    [self.window addSubview:_bgView];
    [_bgView release];
}

//创建转移操作的视图
- (void)lableToTextField
{
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(30, 50, 200 / 3.0, 40)];
    lable.tag = 100;
    lable.textAlignment = NSTextAlignmentCenter;
    lable.backgroundColor = [UIColor lightGrayColor];
    [_bgView addSubview:lable];
    [lable release];
    
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(60 + 200 / 3.0, 50, 200 / 3.0, 40)];
    textField.tag = 101;
    textField.textAlignment = NSTextAlignmentCenter;
    textField.placeholder = @"输入:";
    textField.borderStyle = UITextBorderStyleLine | UITextBorderStyleRoundedRect;
    textField.delegate = self;
    [_bgView addSubview:textField];
    [textField release];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(90 + 200 / 1.5, 50, 200 / 3.0, 40);
//    button.backgroundColor = [UIColor brownColor];
    [button addTarget:self action:@selector(transfer:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"转移" forState:UIControlStateNormal];
//    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.layer.cornerRadius = 8;
    [_bgView addSubview:button];
}

//将textField的内容显示在lable中
- (void)transfer:(UIButton *)button
{
    UILabel *lable = (UILabel *)[_bgView viewWithTag:100];
    UITextField *textField = (UITextField *)[_bgView viewWithTag:101];
    lable.text = textField.text;
    textField.text = nil;
}

//加法
- (void)addMethod
{
    UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(30, 120, 200 / 3.0, 40)];
    textField1.tag = 102;
    textField1.placeholder = @"被加数";
    textField1.textAlignment = NSTextAlignmentCenter;
    textField1.borderStyle = UITextBorderStyleLine | UITextBorderStyleRoundedRect;
    textField1.keyboardType = UIKeyboardTypeNumberPad;
    textField1.delegate = self;
    [_bgView addSubview:textField1];
    [textField1 release];
    
    UILabel *lable1 = [[UILabel alloc] initWithFrame:CGRectMake(30 + 200 / 3.0, 120, 30, 40)];
    lable1.text = @"+";
    lable1.textAlignment = NSTextAlignmentCenter;
    [_bgView addSubview:lable1];
    [lable1 release];
    
    UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(60 + 200 / 3.0, 120, 200 / 3.0, 40)];
    textField2.tag = 103;
    textField2.placeholder = @"加数";
    textField2.textAlignment = NSTextAlignmentCenter;
    textField2.borderStyle = UITextBorderStyleLine | UITextBorderStyleRoundedRect;
    textField2.keyboardType = UIKeyboardTypeNumberPad;
    textField2.delegate = self;
    [_bgView addSubview:textField2];
    [textField2 release];

    
    UILabel *lable2 = [[UILabel alloc] initWithFrame:CGRectMake(60 + 200 / 1.5, 120, 30, 40)];
    lable2.text = @"=";
    lable2.textAlignment = NSTextAlignmentCenter;
    [_bgView addSubview:lable2];
    [lable2 release];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(90 + 200 / 1.5, 120, 200 / 3.0, 40)];
    lable.tag = 104;
    lable.textAlignment = NSTextAlignmentCenter;
    lable.backgroundColor = [UIColor lightGrayColor];
    [_bgView addSubview:lable];
    [lable release];
    
    UIButton *reslut = [UIButton buttonWithType:UIButtonTypeSystem];
    reslut.frame = CGRectMake(110, 190, 100, 40);
//    reslut.backgroundColor = [UIColor brownColor];
    [reslut addTarget:self action:@selector(add:) forControlEvents:UIControlEventTouchUpInside];
    [reslut setTitle:@"计算" forState:UIControlStateNormal];
//    [reslut setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    reslut.layer.cornerRadius = 8;
    [_bgView addSubview:reslut];
}

- (void)add:(UIButton *)button
{
    UITextField *textField1 = (UITextField *)[_bgView viewWithTag:102];
    UITextField *textField2 = (UITextField *)[_bgView viewWithTag:103];
    UILabel *lable = (UILabel *)[_bgView viewWithTag:104];
    int relust = [textField1.text intValue] + [textField2.text intValue];
    lable.text = [NSString stringWithFormat:@"%d", relust];
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)dealloc
{
    [_window release];
    [super dealloc];
}

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值