计算器(没写完呢)

  UILabel *plusLable = [[UILabel alloc] initWithFrame:CGRectMake(40, 40, 200, 40)];
    plusLable.backgroundColor = [UIColor whiteColor];
    plusLable.textAlignment = NSTextAlignmentRight; //所显示的文本内容位置
    plusLable.tag = 100;
    [self.window addSubview:plusLable];
    [plusLable release];


    UIButton *a1Button= [UIButton buttonWithType: UIButtonTypeRoundedRect];
    a1Button.frame = CGRectMake(40, 100, 40, 40);
    [a1Button setTitle:@"1" forState:UIControlStateNormal];
    a1Button.backgroundColor = [UIColor blackColor];
    [a1Button addTarget:self action:@selector(plus1:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:a1Button];
   
    UIButton *a2Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    a2Button.frame = CGRectMake(120, 100, 40, 40);
    [a2Button setTitle:@"2" forState:UIControlStateNormal];
    a2Button.backgroundColor = [UIColor blackColor];
    [a2Button addTarget:self action:@selector(plus1:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:a2Button];
    
    UIButton *a3Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    a3Button.frame = CGRectMake(200, 100, 40, 40);
    [a3Button setTitle:@"3" forState:UIControlStateNormal];
    a3Button.backgroundColor = [UIColor blackColor];
    [a3Button addTarget:self action:@selector(plus1:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:a3Button];
    
    UIButton *a4Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    a4Button.frame = CGRectMake(40,160, 40, 40);
    [a4Button setTitle:@"4" forState:UIControlStateNormal];
    a4Button.backgroundColor = [UIColor blackColor];
    [a4Button addTarget:self action:@selector(plus1:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:a4Button];
    
    UIButton *a5Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    a5Button.frame = CGRectMake(120, 160, 40, 40);
    [a5Button setTitle:@"5" forState:UIControlStateNormal];
    a5Button.backgroundColor = [UIColor blackColor];
    [a5Button addTarget:self action:@selector(plus1:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:a5Button];
    
    UIButton*a6Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    a6Button.frame = CGRectMake(200, 160, 40, 40);
    [a6Button setTitle:@"6" forState:UIControlStateNormal];
    a6Button.backgroundColor = [UIColor blackColor];
    [a6Button addTarget:self action:@selector(plus1:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:a6Button];
    
    UIButton *a7Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    a7Button.frame = CGRectMake(40,220, 40, 40);
    [a7Button setTitle:@"7" forState:UIControlStateNormal];
    a7Button.backgroundColor = [UIColor blackColor];
    [a7Button addTarget:self action:@selector(plus1:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:a7Button];
    
    UIButton *a8Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    a8Button.frame = CGRectMake(120, 220, 40, 40);
    [a8Button setTitle:@"8" forState:UIControlStateNormal];
    a8Button.backgroundColor = [UIColor blackColor];
    [a8Button addTarget:self action:@selector(plus1:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:a8Button];
    
    UIButton * a9Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    a9Button.frame = CGRectMake(200, 220, 40, 40);
    [a9Button setTitle:@"9" forState:UIControlStateNormal];
    a9Button.backgroundColor = [UIColor blackColor];
    [a9Button addTarget:self action:@selector(plus1:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:a9Button];
    
    UIButton *dyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    dyButton.frame = CGRectMake(260, 100, 40, 40);
    [dyButton setTitle:@"=" forState:UIControlStateNormal];
    dyButton.backgroundColor = [UIColor blackColor];
     [dyButton addTarget:self action:@selector(plus1:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:dyButton];
    
    UIButton *sumButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    sumButton.frame = CGRectMake(260, 160, 40, 40);
    [sumButton setTitle:@"+" forState:UIControlStateNormal];
    sumButton.backgroundColor = [UIColor blackColor];
     [sumButton addTarget:self action:@selector(plus1:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:sumButton];
    
    UIButton *jfButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    jfButton.frame = CGRectMake(260, 220, 40, 40);
    [jfButton setTitle:@"-" forState:UIControlStateNormal];
    jfButton.backgroundColor = [UIColor blackColor];
     [sumButton addTarget:self action:@selector(plus1:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:jfButton];

    return YES;
}


- (void)plus1:(UIButton *)button
{
    int sum = 0 ;
    NSString *number1 =[button titleForState:UIControlStateNormal];


    UILabel *lable= (UILabel *)[self.window viewWithTag:100];
 
    int number = number1.intValue;
    if (number >= 0 && number <= 9) {
    
        lable.text = number1  ;
    }
    if ([number1 isEqualToString:@"+"]) {
        sum += number;


    }
    if ([number1 isEqualToString:@"="]) {
        lable.text = [NSString stringWithFormat:@"%d", sum];
      
    }
    
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值