iOS入门开发计算器小程序,包括button、label、image的使用

来源:互联网 发布:hakko编程软件 编辑:程序博客网 时间:2024/06/14 05:02

大家可以查看下自己X-code的版本号:先点击一次X-code,然后按如下操作




点开之后选择最后一栏locations:(例如我的是X-code 5.1.1是比较高的版本了,可以自动release之类的)




接下来就是计算器小程序的编写:

首先给大家看看完成后的界面吧:



代理里面的文件不动:如我的MJAppDalegate.h 和MJAppDalegate.m还是用系统自带的代码函数,我改变的是控制器函数里面的内容;

选中Main.storyboard可以看到如下,该控制器是连接着view的:(View Controller选中右击)




MJViewController.h文件中:


#import <UIKit/UIKit.h>

@interface MJViewController :UIViewController

@property(retain,nonatomic)UIButton *button;

@property(retain,nonatomic)UILabel *label;

@property(retain,nonatomic)NSMutableString *str;

@property(assign,nonatomic)double inputNum,saveNum,outputNum,jumpNum;

@end


MJViewController.m文件中:

//

//  MJViewController.m

//  calculator

//

//  Created by admin on 14-7-28.

//  Copyright (c) 2014 itcast. All rights reserved.

//


#import "MJViewController.h"

@interface MJViewController ()


@end


@implementation MJViewController


@synthesize button,label,str,inputNum,saveNum,outputNum,jumpNum;


- (void)viewDidLoad

{

    [superviewDidLoad];

    

    self.str = [[NSMutableStringalloc]init];

    

    //设置背景图片

    //    NSBundle *bundle = [NSBundle mainBundle];

    //    NSData *data  = [[NSData alloc] initWithContentsOfFile:[bundle pathForResource:@"background" ofType:@"jpg"]];

    //    UIImage *img = [UIImage imageWithData:data];

   UIImage *img = [UIImageimageNamed:@"backgroud.jpg"];

    [self.viewsetBackgroundColor:[UIColorcolorWithPatternImage:img]];//将背景图片设置为background.jpg

    

    

   //添加提示性文字

    UIAlertView *alex = [[UIAlertViewalloc]initWithTitle:@"使用说明" message:@"不支持点击运算符连算" delegate:nilcancelButtonTitle:@"OK"otherButtonTitles:nil];

    [alexshow];

    

    //创建标签

    self.label = [[UILabelalloc]initWithFrame:CGRectMake(90,40,200,50)];

    [self.viewaddSubview:label];

    self.label.backgroundColor = [UIColor clearColor];//清除背景颜色

    self.label.textColor = [UIColor blueColor];//设置字体颜色

    self.label.textAlignmentNSTextAlignmentRight;//字体居右;

    self.label.font = [UIFont systemFontOfSize:32.4];

    

    //添加数字1-9

    NSArray *array = [NSArrayarrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",nil];//nil代表指针结束

   int n =0;

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

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

            self.button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];//设置圆角

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

            [self.buttonsetBackgroundColor:[UIColorwhiteColor]];

            [self.buttonsetTitle:[arrayobjectAtIndex:n++]forState:UIControlStateNormal];

            [self.buttonaddTarget:selfaction:@selector(one:)forControlEvents:UIControlEventTouchUpInside];

            [self.viewaddSubview:self.button];

        }

    }

    

    //添加0按钮

    UIButton *button0 = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    [button0setFrame:CGRectMake(30 ,345,60,60)];

    [button0 setTitle:@"0"forState:UIControlStateNormal];

    [button0 setBackgroundColor:[UIColorwhiteColor]];

    [button0 addTarget:selfaction:@selector(one:)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:button0];

    

    //添加运算符

   NSArray *array1 = [NSArrayarrayWithObjects:@"+",@"-",@"*",@"/",nil];

   int n1 =0;

   for (int i =0;  i<4; i++) {

        self.button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];//设置圆角

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

        [self.buttonsetTitle:[array1objectAtIndex:n1++]forState:UIControlStateNormal];

        [self.buttonaddTarget:selfaction:@selector(two:)forControlEvents:UIControlEventTouchUpInside];

        [self.buttonsetBackgroundColor:[UIColorwhiteColor]];

        [self.viewaddSubview:self.button];

    }

    

    UIButton *buttonPoint = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    [buttonPointsetFrame:CGRectMake(95,345,60,60)];

    [buttonPoint setTitle:@"."forState:UIControlStateNormal];

    [buttonPoint addTarget:selfaction:@selector(one:)forControlEvents:UIControlEventTouchUpInside];

    [buttonPoint setBackgroundColor:[UIColorwhiteColor]];

    [self.viewaddSubview:buttonPoint];

    

    UIButton *button1 = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    [button1setFrame:CGRectMake(160,345,60,60)];

    [button1 setTitle:@"back"forState:UIControlStateNormal];

    [button1 addTarget:selfaction:@selector(back:)forControlEvents:UIControlEventTouchUpInside];

    [button1 setBackgroundColor:[UIColorwhiteColor]];

    [self.viewaddSubview:button1];

    

    //清除按钮

    UIButton *buttonBack=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    [buttonBacksetFrame:CGRectMake(30,410,125,40)];

    [buttonBack setTitle:@"AC"forState:UIControlStateNormal];

    [buttonBack addTarget:selfaction:@selector(clean:)forControlEvents:UIControlEventTouchUpInside];

    [buttonBack setBackgroundColor:[UIColorwhiteColor]];

    [self.viewaddSubview:buttonBack];

    

    

    UIButton *button2=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    [button2setFrame:CGRectMake(160,410,125,40)];

    [button2 setTitle:@"="forState:UIControlStateNormal];

    [button2 addTarget:selfaction:@selector(go:)forControlEvents:UIControlEventTouchUpInside];

    [button2 setBackgroundColor:[UIColorwhiteColor]];

    [self.viewaddSubview:button2];

    // Do any additional setup after loading the view, typically from a nib.

}


-(void) one:(UIButton *)sender//按下数字键时调用的函数

{

    if ([self.strhasPrefix:@"+"]||[self.strhasPrefix:@"-"]||[self.strhasPrefix:@"*"]||[self.strhasPrefix:@"/"]) {

        [self.strsetString:@""];

    }

    

    [self.strappendString:[sendercurrentTitle]];

    

   self.label.text =self.str;

   self.inputNum = [self.label.textdoubleValue];

}


//inputNum,saveNum,outputNum,jumpNum;

- (void)two:(id)sender//按下符号键时调用的函数

{

    [self.strsetString:@""];

    [self.strappendString:[sendercurrentTitle]];

    self.label.text = [NSStringstringWithString:str];

   if([self.strhasPrefix:@"+"])

    {

        self.saveNum =self.inputNum;

       self.jumpNum =1;

    }

   elseif([self.strhasPrefix:@"-"])

    {

        self.saveNum =self.inputNum;

       self.jumpNum =2;

    }

   elseif([self.strhasPrefix:@"*"])

    {

        self.saveNum =self.inputNum;

       self.jumpNum =3;

    }

   elseif([self.strhasPrefix:@"/"])

    {

        self.saveNum =self.inputNum;

       self.jumpNum =4;

    }

}


-(void) go:(id)sender

{

   if (self.jumpNum ==1) {

       self.outputNum =self.saveNum + [self.label.textdoubleValue];

       self.label.text = [NSStringstringWithFormat:@"%f",self.outputNum];//现实计算结果

       self.inputNum = [self.label.textdoubleValue];//保证连加

       self.outputNum =0;

        [self.strsetString:@""];//保证每次结果输出后继续计算,不需要清除,str存数字、运算符的

    }

   elseif (self.jumpNum ==2) {

       self.outputNum =self.saveNum - [self.label.textdoubleValue];

       self.label.text = [NSStringstringWithFormat:@"%f",self.outputNum];//现实计算结果

       self.inputNum = [self.label.textdoubleValue];//

       self.outputNum =0;

        [self.strsetString:@""];//保证每次结果输出后继续计算,不需要清除,str存数字、运算符的

    }

   elseif (self.jumpNum ==3) {

       self.outputNum =self.saveNum * [self.label.textdoubleValue];

       self.label.text = [NSStringstringWithFormat:@"%f",self.outputNum];//现实计算结果

       self.inputNum = [self.label.textdoubleValue];//

       self.outputNum =0;

        [self.strsetString:@""];//保证每次结果输出后继续计算,不需要清除,str存数字、运算符的

    }

   elseif (self.jumpNum ==4) {

       self.outputNum =self.saveNum / [self.label.textdoubleValue];

       self.label.text = [NSStringstringWithFormat:@"%f",self.outputNum];//现实计算结果

       self.inputNum = [self.label.textdoubleValue];//

       self.outputNum =0;

        [self.strsetString:@""];//保证每次结果输出后继续计算,不需要清除,str存数字、运算符的

    }

}


-(void) clean:(id)sender

{

    [self.strsetString:@""];

   self.inputNum =0;

   self.saveNum =0;

   self.outputNum =0;

   self.label.text =@"0";

}


-(void)back:(id)sender//回退一位

{

    if (![self.label.textisEqualToString:@""] && ![self.strisEqualToString:@""]) {

        [self.strdeleteCharactersInRange:NSMakeRange(self.str.length-1,1)];

       self.label.text = [NSStringstringWithString:self.str];

    }

}

- (void)didReceiveMemoryWarning//系统自带,当机器内存不够时释放内存,正常情况不会调用

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


文件到此结束,
运行程序出现如下界面:



0 0
原创粉丝点击