ui计算器

@interface MainViewController : UIViewController


@property (nonatomic, retain)UIButton *button;

@property (nonatomic, retain)UILabel *laber;

@property (nonatomic, assign)NSInteger firstNumber;//记录第一个数字的值

@property (nonatomic, assign)bool buttonType;// 是否点击了解符号键, 默认为NO

@property (nonatomic, assign) NSInteger mathType;//0- 1- 2- 3-


@end



@interface MainViewController ()


@end


@implementation MainViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

   self.laber = [[UILabel alloc] initWithFrame:CGRectMake(20, 80, 280, 50)];

    self.laber.backgroundColor = [UIColor brownColor];

    self.laber.text = @"";

    self.laber.textColor = [UIColor blackColor];

    self.laber.textAlignment = NSTextAlignmentRight;

    [self.view addSubview:self.laber];

    [self.laber release];

    

//    UIButton *numberButton = [UIButton buttonWithType:(UIButtonTypeSystem)];

//    numberButton.frame = CGRectMake(20, 140, 50, 50);

//    [numberButton setTitle:@"2" forState:(UIControlStateNormal)];

//    [numberButton setBackgroundColor:[UIColor brownColor]];

//    [numberButton addTarget:self action:@selector(nuberButtonClicked:) forControlEvents:(UIControlEventTouchUpInside)];

//    [self.view addSubview:numberButton];

   

    //添加1-9数字

    NSArray *array=[NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9", nil];

    int n=0;

    for (int i=0; i<3; i++)

    {

        for (int j=0; j<3; j++)

        {

            self.button=[UIButton buttonWithType:UIButtonTypeRoundedRect];

            self.button.frame=CGRectMake(30+65*j, 150+65*i, 60, 60);

            [self.button setTitle:[array objectAtIndex:n++] forState:UIControlStateNormal];

            [self.view addSubview:self.button];

            [self.button addTarget:self action:@selector(nuberButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

        }

    }


    

    

    UIButton *equal1 = [UIButton buttonWithType:(UIButtonTypeSystem)];

    equal1.frame = CGRectMake(260, 140, 50, 50);

    [equal1 setTitle:@" = " forState:(UIControlStateNormal)];

    [equal1 setBackgroundColor:[UIColor brownColor]];

    [equal1 addTarget:self action:@selector(equal:) forControlEvents:(UIControlEventTouchUpInside)];

    [self.view addSubview:equal1];

    

    UIButton *add1 = [UIButton buttonWithType:(UIButtonTypeSystem)];

    add1.frame = CGRectMake(200, 140, 50, 50);

    [add1 setTitle:@" + " forState:(UIControlStateNormal)];

    [add1 setBackgroundColor:[UIColor blackColor]];

    [add1 addTarget:self action:@selector(add:) forControlEvents:(UIControlEventTouchUpInside)];

    [self.view addSubview:add1];

    

}


//- (void)dealloc

//{

//

//}


//数字键

- (void)nuberButtonClicked:(UIButton *)button

{

    if (self.buttonType == NO) {

      //如果没有点击符号键

        

      // 1.获取到当前的数字

        NSString *number1 = self.laber.text;

        

      // 2.在当前的数字后面拼接一个2

//        self.laber.text =[NSString stringWithFormat:@"%@%@",number1, button.currentTitle];

        

        self.laber.text = [number1 stringByAppendingString:button.currentTitle];

    } else{

    

    //如果刚刚点击了符号键

        self.laber.text = button.titleLabel.text;

    //把符号键状态 取消

        self.buttonType = NO;

    }

}


//符号键

- (void)add:(UIButton *)button

{

    //当点击加号键时 将第一个数字保存

    self.firstNumber = [self.laber.text integerValue];

    self.buttonType = YES;//已经点击了符号键

    self.mathType = 0;// 当前点击的是加号

    

}


// 等号键

- (void)equal:(UIButton *)button

{

    NSInteger secondNumber = [self.laber.text integerValue];

    

    //计算

    if (0 == self.mathType) {

    NSInteger result = secondNumber + self.firstNumber;

        //将结果赋给laber显示


    self.laber.text = [NSString stringWithFormat:@"%d", result];

}

}

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值