OC中使用UI自定义控件实现计算器的设计(版本1简单的加减乘除,连加,连减,连除,连乘)

OC中使用UI自定义控件实现计算器的设计(版本1简单的加减乘除,连加,连减,连除,连乘)
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIApplicationDelegate>

{
    float _operator1;
    float _operator2;
    float _result;
}
@end
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UITextField * textFied1 = [[UITextField alloc]initWithFrame:CGRectMake(10, 30, 300 ,50)];
    textFied1.borderStyle = UITextBorderStyleLine;
    textFied1.backgroundColor = [UIColor redColor];
    [self.view addSubview:textFied1];
    textFied1.clearButtonMode = UITextFieldViewModeAlways;
    textFied1.tag = 007;

    UIButton * btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.Frame=CGRectMake(10, 90, 70, 70);
    btn1.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:btn1];
    [btn1 setTitle:@"7" forState:UIControlStateNormal];

    [btn1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    btn1.tag = 7;

    UIButton * btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn2.Frame=CGRectMake(85, 90, 70, 70);
    btn2.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:btn2];
    [btn2 setTitle:@"8" forState:UIControlStateNormal];
    btn2.tag = 8;

    [btn2 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

    UIButton * btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn3.Frame=CGRectMake(160, 90, 70, 70);
    btn3.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:btn3];
    [btn3 setTitle:@"9" forState:UIControlStateNormal];
    btn3.tag = 9;

   [btn3 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

    UIButton * btn4 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn4.Frame=CGRectMake(235, 90, 70, 70);
    btn4.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:btn4];
    [btn4 setTitle:@"/" forState:UIControlStateNormal];
    btn4.tag = 13;

   [btn4 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

    UIButton * btn5 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn5.Frame=CGRectMake(10, 165, 70, 70);
    btn5.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:btn5];
    [btn5 setTitle:@"4" forState:UIControlStateNormal];
    btn5.tag = 4;

    [btn5 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

    UIButton * btn6 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn6.Frame=CGRectMake(85, 165, 70, 70);
    btn6.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:btn6];
    [btn6 setTitle:@"5" forState:UIControlStateNormal];
    btn6.tag = 5;
    [btn6 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

    UIButton * btn7 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn7.Frame=CGRectMake(160, 165, 70, 70);
    btn7.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:btn7];
     [btn7 setTitle:@"6" forState:UIControlStateNormal];
    btn7.tag = 6;
   [btn7 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

    UIButton * btn8 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn8.Frame=CGRectMake(235, 165, 70, 70);
    btn8.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:btn8];
     [btn8 setTitle:@"X" forState:UIControlStateNormal];
    btn8.tag = 12;
    [btn8 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

    UIButton * btn9 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn9.Frame=CGRectMake(10, 240, 70, 70);
    btn9.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:btn9];
    [btn9 setTitle:@"1" forState:UIControlStateNormal];
    btn9.tag = 1;
     [btn9 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

    UIButton * btn10 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn10.Frame=CGRectMake(85, 240, 70, 70);
    btn10.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:btn10];
     [btn10 setTitle:@"2" forState:UIControlStateNormal];
    btn10.tag = 2;
    [btn10 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

    UIButton * btn11 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn11.Frame=CGRectMake(160, 240, 70, 70);
    btn11.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:btn11];
     [btn11 setTitle:@"3" forState:UIControlStateNormal];
    btn11.tag = 3;
   [btn11 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

    UIButton * btn12 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn12.Frame=CGRectMake(235, 240, 70, 70);
    btn12.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:btn12];
     [btn12 setTitle:@"-" forState:UIControlStateNormal];
    btn12.tag = 11;
   [btn12 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

    UIButton * btn13 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn13.Frame=CGRectMake(10, 315, 70, 70);
    btn13.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:btn13];
     [btn13 setTitle:@"0" forState:UIControlStateNormal];
    btn13.tag = 0;
   [btn13 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

    UIButton * btn14 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn14.Frame=CGRectMake(85, 315, 145, 70);
    btn14.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:btn14];
     [btn14 setTitle:@"=" forState:UIControlStateNormal];
    btn14.tag = 14;
   [btn14 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

    UIButton * btn15 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn15.Frame=CGRectMake(235, 315, 70, 70);
    btn15.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:btn15];
    [btn15 setTitle:@"+" forState:UIControlStateNormal];
    btn15.tag = 10;
    [btn15 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

    // Do any additional setup after loading the view, typically from a nib.
}

//enum calCul
//{
//    add,
//    sub,
//    mul,
//    mod
//};
//
//typedef enum calCul calCul;
int count;

-(void)btnClick:(UIButton *)btn
{
    NSString * str = [[NSString alloc]init];
    if (btn.tag == 0) {
        UITextField * field = ( UITextField * )[self.view viewWithTag:007];
        str = @"0";
        //_operator1 = 0;

        field.text = str;
    }
    else if(btn.tag==1)
    {
        UITextField * field = ( UITextField * )[self.view viewWithTag:007];
        str = @"1";
        //_operator2 = 1;
        field.text = str;
    }
    else if(btn.tag==2)
    {
        UITextField * field = ( UITextField * )[self.view viewWithTag:007];
        str = @"2";
        field.text = str;
    }
    else if(btn.tag==3)
    {
        UITextField * field = ( UITextField * )[self.view viewWithTag:007];
        str = @"3";
        field.text = str;
    }
    else if(btn.tag==4)
    {
        UITextField * field = ( UITextField * )[self.view viewWithTag:007];
        str = @"4";
        field.text = str;
    }
    else if(btn.tag==5)
    {
        UITextField * field = ( UITextField * )[self.view viewWithTag:007];
        str = @"5";
        field.text = str;
    }
    else if(btn.tag==6)
    {
        UITextField * field = ( UITextField * )[self.view viewWithTag:007];
        str = @"6";
        field.text = str;
    }
    else if(btn.tag==7)
    {
        UITextField * field = ( UITextField * )[self.view viewWithTag:007];
        str = @"7";
        field.text = str;
    }    else if(btn.tag==8)
    {
        UITextField * field = ( UITextField * )[self.view viewWithTag:007];
        str = @"8";
        field.text = str;
    }
    else if(btn.tag==9)
    {
        UITextField * field = ( UITextField * )[self.view viewWithTag:007];
        str = @"9";
        field.text = str;
    }
    else if(btn.tag==10)
    {
        count = 1;
        UITextField * field = ( UITextField * )[self.view viewWithTag:007];
        //str = @"+";
        str = field.text;
        //_result = _operator1;
        _operator1 = [str doubleValue];

        //str = [[NSString alloc]initWithFormat:@"%.2f",_result];
        field.text = str;
    }
    else if(btn.tag==11)
    {
        count = 2;
        UITextField * field = ( UITextField * )[self.view viewWithTag:007];
        //str = @"-";
        //_result = _operator1 + _operator2;
        str = field.text;
        _operator1 = [str doubleValue];
        field.text = str;
    }
    else if(btn.tag==12)
    {
        count = 3;
        UITextField * field = ( UITextField * )[self.view viewWithTag:007];
        //str = @"*";
        str = field.text;
        _operator1 = [str doubleValue];
        field.text = str;
    }
    else if(btn.tag==13)
    {
        count = 4;
        UITextField * field = ( UITextField * )[self.view viewWithTag:007];
        //str = @"/";
        str = field.text;
        _operator1 = [str doubleValue];
        field.text = str;
    }
    else if(btn.tag==14)
    {
        UITextField * field = ( UITextField * )[self.view viewWithTag:007];
        str = @"=";
        str = field.text;
        _operator2 = [str doubleValue];
        if (count==1) {
            _result = _operator1 + _operator2;
        }
        else if(count == 2)
        {
        _result = _operator1 - _operator2;
        }
        else if(count == 3)
        {
        _result = _operator1 * _operator2;
        }
        else if(count==4)
        {
        _result = _operator1 / _operator2;
        }
        str = [[NSString alloc]initWithFormat:@"%.6f",_result];
        field.text = str;
    }
    }
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-25 19:16:47

OC中使用UI自定义控件实现计算器的设计(版本1简单的加减乘除,连加,连减,连除,连乘)的相关文章

OC中面向对象2

一. 定义OC的类和创建OC的对象 接下来就在OC中模拟现实生活中的情况,创建一辆车出来.首先要有一个车子类,然后再利用车子类创建车子对象 要描述OC中的类稍微麻烦一点,分2大步骤:类的声明.类的实现(定义).跟函数类似,函数有分声明和定义 1. 类的声明 1) 代码编写 定义一个Car类,拥有2个属性:轮子数.时速,1个行为:跑 类名\属性的命名规则:标示符的规则 类名的命名规范:有意义.驼峰标识.首字母大写 #import <Foundation/Foundation.h> // 类的声明

OC中的NSDate日期类和Foundation框架中的结构体相关操作

一:NSDate NSDate可以?用来表?示时间,可以进?行?一些常见的?日期\时间处理 • ?一个NSDate对象就代表?一个时间• [NSDate date]返回的就是当前时间 1)日期对象的格式化操作 // 1,获取当前时间 // NSDate *date = [NSDate date]; // NSLog(@"date = %@", date); // // // 格式化输出时间 //// y : 年 //// // M : 月 //// // d : 日 //// // h

OC基础--OC中的类方法和对象方法

PS:个人感觉跟C#的静态方法和非静态方法有点类似,仅仅是有点类似.明杰老师说过不要总跟之前学过的语言做比较,但是个人觉得,比较一下可以加深印象吧.重点是自己真的能够区分开! 一.OC中的对象方法 1.以减号“-”开头 2.只能让对象调用,没有对象,这个方法根本不可能被执行 3.对象方法能访问实例变量(也就是成员变量) 二.OC中的类方法 1.以加号“+”开头 2.只能用类名调用,对象不能调用 3.类方法中不能访问实例变量(也就是成员变量) 4.适用场合:当不需要访问成员变量的时候,尽量用类方法

OC 中NULL nil和NSNULL的区别

一.nil:nil是oc的,nil是一个空的对象,指针地址指向的是NULL 在oc中,可以给nil发送任何消息,都不会报错 二.NULL:是c的,空地址,数字,本身的数值是0  不能给NULL消息 三.NSNULL :是为了向数组或者字典中添加空值使用的 从Xcode6开始,取消了NULL的只能提示,因为swift 中没有NULL的概念,只有nil -在OC中,使用NULL或者nil都不会出错 -但是:从概念的角度而言,**(指针的指针)的参数,不想获取对ui内容,应该传入NULL -OC中提供

OC中多线程的一些概念

1.进程1.1>进程是指在系统中正在运行的一个应用程序(同时打开QQ和Xcode,系统会分别启动2个进程)1.2>每个进程之间是独立的,每个进程均运行在其专用的且受保护的内存空间内 2.线程1.1>一个进程想要执行任务,必须得有线程(每个进程至少要有一条线程,即主线程)1.2>线程是进程的基本执行单元,进程的所有任务都在线程中执行3.多线程3.1>一个进程中可以开启多条线程,每条线程可以并行(同时)执行不同的任务      进程→车间, 线程→车间工人3.2>多线程技术

OC中SEL,类别,继承,协议的使用

1.SEL SEL是selector的缩写,selector在OC中作用是定义一个方法变量,通过该方法变量来调用方法.我们在后面的UI中会经常用selector来调用事件方法.下面我将举两个例子来说明SEL的使用. 直接通过方法名来调用,我们先新建一个Student类,并在其中定义一个名叫Test的对象方法.并定义一个输出语句: -(void)test { NSLog(@"这是SEL的测试方法"); } 我们在Main方法中通过SEL调用就会输出以上结果,如下: SEL s = @se

OC中并发编程的相关API和面临的挑战

OC中并发编程的相关API和面临的挑战(1) 小引 http://www.objc.io/站点主要以杂志的形式,深入挖掘在OC中的最佳编程实践和高级技术,每个月探讨一个主题,每个主题都会有几篇相关的文章出炉,2013年7月份的主题是并发编程,今天挑选其中的第2篇文章(Concurrent Programming: APIs and Challenges)进行翻译,与大家分享一下主要内容.由于内容比较多,我将分两部分翻译(API和难点)完成,翻译中,如有错误,还请指正. 目录 1.介绍 2.OS

为什么我们可以在非UI线程中更新UI

尊重原创转载请注明:From AigeStudio(http://blog.csdn.net/aigestudio)Power by Aige 侵权必究! 炮兵镇楼 看到这样的标题--估计N多人会说我是逗比----因为很多盆友在学习Android(特别是从4.0之后开始入门的)的时候都会常看见或听到别人说我们更新UI呢要在UI线程(或者说主线程)中去更新UI,不要在子线程中更新UI,而Android官方呢也建议我们不要在非UI线程直接更新UI,为什么呢?借助Android官方的一句话来说就是:

关于OC中直接打印结构体,点(CGRect,CGSize,CGPoint,UIOffset)等数据类型

关于OC直接打印结构体,点(CGRect,CGSize,CGPoint,UIOffset)等数据类型,我们完全可以把其转换为OC对象来进项打印调试,而不必对结构体中的成员变量进行打印.就好比我们可以使用NSStringFromCGRect(CGRect rect)来直接打印一个结构体,其他打印可以参考以下内容 UIKIT_EXTERN NSString *NSStringFromCGPoint(CGPoint point); UIKIT_EXTERN NSString *NSStringFrom