基于UI写的简单的计算器

本文介绍了如何使用UIViewController和UIButton创建一个基本的计算器应用。通过UI布局和事件响应,实现了数字输入和基本运算功能。
摘要由CSDN通过智能技术生成
</pre><span style="font-size:24px;"></span><div style="text-align: center;">主要代码</div><div style="text-align: left;">.h文件</div><div style="text-align: left;"><pre name="code" class="cpp">#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property(retain,nonatomic)UIButton *button;
@property(retain,nonatomic)UILabel *label;
@property(retain,nonatomic)NSMutableString *string;
@property(assign,nonatomic)double num1,num2,num3,num4;
@end
.m文件
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize button,label,string,num1,num2,num3,num4;//string保存字符,显示数值。num1是存输入的数值,num2是存运算符前的数值,num3是运算结果,num4是判断进行何种运算
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //设置背景图片
    NSBundle *bundle=[NSBundle mainBundle];
    NSData *data=[[NSData alloc]initWithContentsOfFile:
                  [bundle pathForResource:@"3" ofType:@"jpg"]];//找到NSBundle的某一资源
    UIImage *img=[UIImage imageWithData:data];//创建了可用的图像对象
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:img]];//UIColor colorWithPatternImage:方法是把图片转化为color类型  将背景换做提供的图片
    
    //创建标签
    self.label=[[UILabel alloc]initWithFrame:CGRectMake(90, 40, 200, 50)];
    [self.view addSubview:label];
    self.label.backgroundColor=[UIColor clearColor];//清空背景颜色
    self.label.textColor=[UIColor blueColor];//字体颜色
    self.label.textAlignment =  NSTextAlignmentRight;;//字体居右
    self.label.font=[UIFont systemFontOfSize:32.4];
    
    //添加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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值