ios学习记录 day UI计算器

//  AppDelegate.h

#import <UIKit/UIKit.h>
enum fuhaoButton
{
    add,
    sub,
    mul,//乘
    divi,//除
    empty
};

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    CGFloat _prefixValue;//上次输入的内容
    BOOL _lastEnterType;//上次输入的类型 YES代表操作符 NO代表数字 默认是NO
}
@property (retain, nonatomic) UIWindow *window;

@property (assign, nonatomic) int fuhao;
@property (retain, nonatomic) UILabel * label;
@property (retain, nonatomic) UIButton * button1;
@property (retain, nonatomic) UIButton * button2;
@property (retain, nonatomic) UIButton * button3;
@property (retain, nonatomic) UIButton * button4;
@property (retain, nonatomic) UIButton * button5;
@property (retain, nonatomic) UIButton * button6;
@property (retain, nonatomic) UIButton * button7;
@property (retain, nonatomic) UIButton * button8;
@property (retain, nonatomic) UIButton * button9;
@property (retain, nonatomic) UIButton * button0;


@end


//  AppDelegate.m


#import "AppDelegate.h"

@implementation AppDelegate
- (void)dealloc
{
    [_window release];
    _window = nil;
    
    [_label release];
    _label = nil;
    
    [_button0 release];
    _button0 = nil;
    [_button1 release];
    _button1 = nil;
    [_button2 release];
    _button2 = nil;
    [_button3 release];
    _button3 = nil;
    [_button4 release];
    _button4 = nil;
    [_button5 release];
    _button5 = nil;
    [_button6 release];
    _button6 = nil;
    [_button7 release];
    _button7 = nil;
    [_button8 release];
    _button8 = nil;
    [_button9 release];
    _button9 = nil;
    
    [super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
    _lastEnterType = NO;//在程序开始的时候设置初始值
    _fuhao = empty;//初始化枚举值 fuhao
    
    //显示用的静态文本框
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(10, 40, 300, 45)];
    [_label setTextAlignment:NSTextAlignmentRight];//静态文本框右对齐
    [_label setLineBreakMode:NSLineBreakByTruncatingHead];//文字超出label显示区的截取方式:省略开始用...代替(...xyz)
    
    _label.backgroundColor = [UIColor grayColor];
    _label.layer.borderWidth = 4;
    _label.layer.borderColor =[UIColor yellowColor].CGColor;
    
    [self.window addSubview:_label];
    [_label release];
    
    //1 按钮
    self.button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [_button1 setFrame:CGRectMake(20, 95, 60, 60)];
    [_button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _button1.layer.borderWidth = 2;
    _button1.layer.borderColor = [UIColor blueColor].CGColor;
    [_button1 setTitle:@"1" forState:UIControlStateNormal];
    [_button1 addTarget:self action:@selector(buttonAction1) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:_button1];
    
    //2 按钮
    self.button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [_button2 setFrame:CGRectMake(90, 95, 60, 60)];
    [_button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _button2.layer.borderWidth = 2;
    _button2.layer.borderColor = [UIColor blueColor].CGColor;
    [_button2 setTitle:@"2" forState:UIControlStateNormal];
    [_button2 addTarget:self action:@selector(buttonAction2) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:_button2];
    
    //3 按钮
    self.button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    [_button3 setFrame:CGRectMake(160, 95, 60, 60)];
    [_button3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _button3.layer.borderWidth = 2;
    _button3.layer.borderColor = [UIColor blueColor].CGColor;
    [_button3 setTitle:@"3" forState:UIControlStateNormal];
    [_button3 addTarget:self action:@selector(buttonAction3) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:_button3];
    
    //4 按钮
    self.button4 = [UIButton buttonWithType:UIButtonTypeCustom];
    [_button4 setFrame:CGRectMake(20, 165, 60, 60)];
    [_button4 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _button4.layer.borderWidth = 2;
    _button4.layer.borderColor = [UIColor blueColor].CGColor;
    [_button4 setTitle:@"4" forState:UIControlStateNormal];
    [_button4 addTarget:self action:@selector(buttonAction4) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:_button4];
    
    //5 按钮
    self.button5 = [UIButton buttonWithType:UIButtonTypeCustom];
    [_button5 setFrame:CGRectMake(90, 165, 60, 60)];
    [_button5 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _button5.layer.borderWidth = 2;
    _button5.layer.borderColor = [UIColor blueColor].CGColor;
    [_button5 setTitle:@"5" forState:UIControlStateNormal];
    [_button5 addTarget:self action:@selector(buttonAction5) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:_button5];
    
    //6 按钮
    self.button6 = [UIButton buttonWithType:UIButtonTypeCustom];
    [_button6 setFrame:CGRectMake(160, 165, 60, 60)];
    [_button6 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _button6.layer.borderWidth = 2;
    _button6.layer.borderColor = [UIColor blueColor].CGColor;
    [_button6 setTitle:@"6" forState:UIControlStateNormal];
    [_button6 addTarget:self action:@selector(buttonAction6) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:_button6];
    
    //7 按钮
    self.button7 = [UIButton buttonWithType:UIButtonTypeCustom];
    [_button7 setFrame:CGRectMake(20, 235, 60, 60)];
    [_button7 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _button7.layer.borderWidth = 2;
    _button7.layer.borderColor = [UIColor blueColor].CGColor;
    [_button7 setTitle:@"7" forState:UIControlStateNormal];
    [_button7 addTarget:self action:@selector(buttonAction7) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:_button7];
    
    //8 按钮
    self.button8 = [UIButton buttonWithType:UIButtonTypeCustom];
    [_button8 setFrame:CGRectMake(90, 235, 60, 60)];
    [_button8 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _button8.layer.borderWidth = 2;
    _button8.layer.borderColor = [UIColor blueColor].CGColor;
    [_button8 setTitle:@"8" forState:UIControlStateNormal];
    [_button8 addTarget:self action:@selector(buttonAction8) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:_button8];
    
    //9 按钮
    self.button9 = [UIButton buttonWithType:UIButtonTypeCustom];
    [_button9 setFrame:CGRectMake(160, 235, 60, 60)];
    [_button9 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _button9.layer.borderWidth = 2;
    _button9.layer.borderColor = [UIColor blueColor].CGColor;
    [_button9 setTitle:@"9" forState:UIControlStateNormal];
    [_button9 addTarget:self action:@selector(buttonAction9) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:_button9];
    
    //0 按钮
    self.button0 = [UIButton buttonWithType:UIButtonTypeCustom];
    [_button0 setFrame:CGRectMake(90, 305, 60, 60)];
    [_button0 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _button0.layer.borderWidth = 2;
    _button0.layer.borderColor = [UIColor blueColor].CGColor;
    [_button0 setTitle:@"0" forState:UIControlStateNormal];
    [_button0 addTarget:self action:@selector(buttonAction0) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:_button0];
    
    //= 按钮
    UIButton * buttonAmount = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonAmount setFrame:CGRectMake(160, 305, 60, 60)];
    [buttonAmount setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    buttonAmount.layer.borderWidth = 2;
    buttonAmount.layer.borderColor = [UIColor blueColor].CGColor;
    [buttonAmount setTitle:@"=" forState:UIControlStateNormal];
    [buttonAmount addTarget:self action:@selector(buttonAmount) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:buttonAmount];
    
    //+ 按钮
    UIButton * buttonAdd = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonAdd setFrame:CGRectMake(230, 95, 60, 60)];
    [buttonAdd setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    buttonAdd.layer.borderWidth = 2;
    buttonAdd.layer.borderColor = [UIColor blueColor].CGColor;
    [buttonAdd setTitle:@"+" forState:UIControlStateNormal];
    [buttonAdd addTarget:self action:@selector(buttonAdd) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:buttonAdd];
    
    //- 按钮
    UIButton * buttonSub = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonSub setFrame:CGRectMake(230, 165, 60, 60)];
    [buttonSub setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    buttonSub.layer.borderWidth = 2;
    buttonSub.layer.borderColor = [UIColor blueColor].CGColor;
    [buttonSub setTitle:@"-" forState:UIControlStateNormal];
    [buttonSub addTarget:self action:@selector(buttonSub) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:buttonSub];
    
    //* 按钮
    UIButton * buttonMul = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonMul setFrame:CGRectMake(230, 235, 60, 60)];
    [buttonMul setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    buttonMul.layer.borderWidth = 2;
    buttonMul.layer.borderColor = [UIColor blueColor].CGColor;
    [buttonMul setTitle:@"*" forState:UIControlStateNormal];
    [buttonMul addTarget:self action:@selector(buttonMul) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:buttonMul];
    
    // / 按钮
    UIButton * buttonDivi = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonDivi setFrame:CGRectMake(230, 305, 60, 60)];
    [buttonDivi setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    buttonDivi.layer.borderWidth = 2;
    buttonDivi.layer.borderColor = [UIColor blueColor].CGColor;
    [buttonDivi setTitle:@"/" forState:UIControlStateNormal];
    [buttonDivi addTarget:self action:@selector(buttonDivi) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:buttonDivi];
    
    // 清除键
    UIButton * buttonClean = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonClean setFrame:CGRectMake(20, 305, 60, 60)];
    [buttonClean setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    buttonClean.layer.borderWidth = 2;
    buttonClean.layer.borderColor = [UIColor blueColor].CGColor;
    [buttonClean setTitle:@"C" forState:UIControlStateNormal];
    [buttonClean addTarget:self action:@selector(buttonClean) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:buttonClean];
    
    
    [self.window makeKeyAndVisible];
    return YES;
}
- (void)buttonAction1
{
    NSString * value = self.button1.currentTitle;//按钮上显示的内容 赋给value
    
    if (_lastEnterType) {//相当于if(_lastEnterTypr == YES) 代码规范不写==
        [_label setText:value];
        _lastEnterType = NO;//改变BOOL值 能够连续输入
    }else{
        if (_label.text == nil) {//显示的时候没有(null)111111的(null)
            [_label setText:@""];//_label.text 赋个空字符串
        }
        NSString * endValue = [NSString stringWithFormat:@"%@%@",_label.text,value];//字符串拼接 用于连续显示
        [_label setText:endValue];//显示
    }
}
- (void)buttonAction2
{
    NSString * value = self.button2.currentTitle;
    if (_lastEnterType) {
        [_label setText:value];
        _lastEnterType = NO;
    }else{
        if (_label.text == nil) {
            [_label setText:@""];
        }
        NSString * endValue = [NSString stringWithFormat:@"%@%@",_label.text,value];
        [_label setText:endValue];
    }
}
- (void)buttonAction3
{
    NSString * value = self.button3.currentTitle;
    if (_lastEnterType) {
        [_label setText:value];
        _lastEnterType = NO;
    }else{
        if (_label.text == nil) {
            [_label setText:@""];
        }
        NSString * endValue = [NSString stringWithFormat:@"%@%@",_label.text,value];
        [_label setText:endValue];
    }
}
- (void)buttonAction4
{
    NSString * value = self.button4.currentTitle;
    if (_lastEnterType) {
        [_label setText:value];
        _lastEnterType = NO;
    }else{
        if (_label.text == nil) {
            [_label setText:@""];
        }
        NSString * endValue = [NSString stringWithFormat:@"%@%@",_label.text,value];
        [_label setText:endValue];
    }
}
- (void)buttonAction5
{
    NSString * value = self.button5.currentTitle;
    if (_lastEnterType) {
        [_label setText:value];
        _lastEnterType = NO;
    }else{
        if (_label.text == nil) {
            [_label setText:@""];
        }
        NSString * endValue = [NSString stringWithFormat:@"%@%@",_label.text,value];
        [_label setText:endValue];
    }
}
- (void)buttonAction6
{
    NSString * value = self.button6.currentTitle;
    if (_lastEnterType) {
        [_label setText:value];
        _lastEnterType = NO;
    }else{
        if (_label.text == nil) {
            [_label setText:@""];
        }
        NSString * endValue = [NSString stringWithFormat:@"%@%@",_label.text,value];
        [_label setText:endValue];
    }
}
- (void)buttonAction7
{
    NSString * value = self.button7.currentTitle;
    if (_lastEnterType) {
        [_label setText:value];
        _lastEnterType = NO;
    }else{
        if (_label.text == nil) {
            [_label setText:@""];
        }
        NSString * endValue = [NSString stringWithFormat:@"%@%@",_label.text,value];
        [_label setText:endValue];
    }
}
- (void)buttonAction8
{
    NSString * value = self.button8.currentTitle;
    if (_lastEnterType) {
        [_label setText:value];
        _lastEnterType = NO;
    }else{
        if (_label.text == nil) {
            [_label setText:@""];
        }
        NSString * endValue = [NSString stringWithFormat:@"%@%@",_label.text,value];
        [_label setText:endValue];
    }
}
- (void)buttonAction9
{
    NSString * value = self.button9.currentTitle;
    if (_lastEnterType) {
        [_label setText:value];
        _lastEnterType = NO;
    }else{
        if (_label.text == nil) {
            [_label setText:@""];
        }
        NSString * endValue = [NSString stringWithFormat:@"%@%@",_label.text,value];
        [_label setText:endValue];
    }
}
- (void)buttonAction0
{
    NSString * value = self.button0.currentTitle;
    if (_lastEnterType) {
        [_label setText:value];
        _lastEnterType = NO;
    }else{
        if (_label.text == nil) {
            [_label setText:@""];
        }
        NSString * endValue = [NSString stringWithFormat:@"%@%@",_label.text,value];
        [_label setText:endValue];
    }
}

- (void)buttonAdd
{
    NSString * current = _label.text;//定义一个变量保存目前显示的内容
    _prefixValue = [current floatValue];//上次文本内容 转成浮点型
    _lastEnterType = YES;
    _fuhao = add;
    
    
}
- (void)buttonSub
{
    NSString * current = _label.text;//定义一个变量保存目前显示的内容
    _prefixValue = [current floatValue];//上次文本内容 转成浮点型
    _lastEnterType = YES;
    _fuhao = sub;
}
- (void)buttonMul
{
    NSString * current = _label.text;//定义一个变量保存目前显示的内容
    _prefixValue = [current floatValue];//上次文本内容 转成浮点型
    _lastEnterType = YES;
    _fuhao = mul;
}
- (void)buttonDivi
{
    NSString * current = _label.text;//定义一个变量保存目前显示的内容
    _prefixValue = [current floatValue];//上次文本内容 转成浮点型
    _lastEnterType = YES;
    _fuhao = divi;
}
- (void)buttonClean
{
    [_label setText:@"0"];
    _lastEnterType = YES;
}
- (void)buttonAmount//点击等号触发的方法
{
    NSString * current = _label.text;
    CGFloat currentValue = [current floatValue];//当前文本转换成浮点型
   
    CGFloat endValue = 0;
    NSString * endStr = @"";
    switch (_fuhao) {
        case add:
            endValue = _prefixValue + currentValue;//之前的 + 之后的
            break;
        case sub:
            endValue = _prefixValue - currentValue;//之前的 - 之后的
            break;
        case mul:
            endValue = _prefixValue * currentValue;//之前的 * 之后的
            break;
        case divi:
            if (currentValue == 0) {
                endValue = _prefixValue;//如果除数为0 显示原来的数 本次运算记为无效
            }else{
                endValue = _prefixValue / currentValue;//之前的 / 之后的
            }
            break;
        default:
            break;
    }
    endStr = [NSString stringWithFormat:@"%f",endValue];//定义一个字符串接受和的值 相当于把结果转换成字符串
    [_label setText:endStr];
    _lastEnterType = YES;
}
- (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:.
}

@end

原文地址:https://www.cnblogs.com/lxllanou/p/3648717.html