步骤:
1.打开Xcode,单机Creat a new Xcode project
2.左边选择ios下Application,右边选择single view Application
3.填写项目名称单机Next
4.ViewController.h中定义成员和方法
#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;
@property(assign,nonatomic)NSString*str;
@end
5.ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize button,label,string,num1,num2,num3,str;//string保存字符,显示数值。num1是存输入的数值,num2是存运算符前的数值,num3是运算结果,str是判断进行何种运算
- (void)viewDidLoad
{
[super viewDidLoad];
//设置背景图片
NSBundle *bundle=[NSBundle mainBundle];
NSData *data=[[NSData alloc]initWithContentsOfFile:
[bundle pathForResource:@"1" ofType:@"jpg"]];//找到NSBundle的某一资源
UIImage *img=[UIImage imageWithData:data];//创建了可用的图像对象
[self.view setBackgroundColor:[UIColor colorWithPatternImage:img]];//UIColor colorWithPatternImage:方法是把图片转化为color类型 将背景换做提供的图片
//添加提示性文字
UIAlertView *alex=[[UIAlertView alloc]initWithTitle:@"使用说明" message:@"只支持两数的计算以及在此基础上的计算,不支持连算。" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alex show];
[alex release];
//创建标签
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=UITextAlignmentRight;//字体居右
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 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:button];
[self.button addTarget:self action:@selector(one:) forControlEvents:UIControlEventTouchUpInside];
}
}
//单独添加0
UIButton *button0=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button0 setFrame:CGRectMake(30, 345, 60, 60)];
[button0 setTitle:@"0" forState:UIControlStateNormal];
[button0 addTarget:self action:@selector(one:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button0];
//添加运算符
NSArray *array1=[NSArray arrayWithObjects:@"+",@"-",@"*",@"/",nil];
for (int i=0; i<4; i++)
{
UIButton *button1=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 setFrame:CGRectMake(225, 150+65*i, 60, 60)];
[button1 setTitle:[array1 objectAtIndex:i] forState:UIControlStateNormal];