第一个APP:IOS做简单运算的计算器

简介:   步骤: 1.打开Xcode,单机Creat a new Xcode project 2.左边选择ios下Application,右边选择single view Application 3.

 

 

步骤:

1.打开Xcode,单机Creat a new Xcode project

2.左边选择ios下Application,右边选择single view Application

3.填写项目名称单机Next

4.ViewController.h中定义成员和方法

//
//  ViewController.h
//  TestCalculator4
//
//  Created by heyonggang on 13-11-28.
//  Copyright (c) 2013年 MiracleHe. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@property(retain,nonatomic) UIButton *button;
@property(retain,nonatomic) UILabel *label;
@property(retain,nonatomic) NSMutableString *string;  //NSMutableString用来处理可变对象,如需要处理字符串并更改字符串中的字符
@property(assign,nonatomic) double num1,num2;
@property(assign,nonatomic) NSString *str;

@end

 

5.ViewController.m

//
//  ViewController.m
//  TestCalculator4
//
//  Created by heyonggang on 13-11-28.
//  Copyright (c) 2013年 MiracleHe. All rights reserved.
//

#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //创建标签
    self.label=[[UILabel alloc]initWithFrame:CGRectMake(90, 40, 200, 50)];
    [self.view addSubview:_label];
    self.label.backgroundColor=[UIColor greenColor];   //设置背景颜色
    self.label.textColor=[UIColor blackColor];         //字体颜色
    //self.label.textAlignment = UITextAlignmentRight; //字体居右
    self.label.textAlignment = NSTextAlignmentCenter;
    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];   //注意:[array objectAtIndex:n++]
            [self.view addSubview:_button];
            [self.button addTarget:self action:@selector(shuzi:) forControlEvents:UIControlEventTouchUpInside]; //addTarget:self 的意思是说,这个方法在本类中也可以传入其他类的指针
        }
    }
    
    
    //单独添加0
    UIButton *button0=[UIButton buttonWithType:UIButtonTypeCustom];  //创建一个圆角矩形的按钮
/*
    CALayer *layer = button0.layer;
    [layer setMasksToBounds:YES];
    [layer setCornerRadius:5.0];
    button0.layer.borderColor = [UIColor lightGrayColor].CGColor;
    button0.layer.borderWidth = 1.0f;
 */
    [button0 setFrame:CGRectMake(30, 345, 60, 60)];                       //设置button在view上的位置
    //也可以这样用:button0.frame:CGRectMake(30, 345, 60, 60);
    [button0 setTitle:@"0" forState:UIControlStateNormal];                //设置button主题
    button0.titleLabel.textColor = [UIColor blackColor];       //设置0键的颜色
    [button0 addTarget:self action:@selector(shuzi:) forControlEvents:UIControlEventTouchUpInside]; //按下按钮,并且当手指离开离开屏幕的时候触发这个事件
                                                                                //触发了这个事件后,执行shuzi方法,action:@selector(shuzi:)
    [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];
        //[array1 objectAtIndex:i]为获取按钮的属性值
        [self.view addSubview:button1];
        [button1 addTarget:self action:@selector(go:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    //添加=
    UIButton *button2=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button2 setFrame:CGRectMake(160, 410, 125, 35)];
    [button2 setTitle:@"=" forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(go:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];
    
    
    //添加清除键
    UIButton *button3=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button3 setFrame:CGRectMake(30, 410, 125, 35)];
    [button3 setTitle:@"AC" forState:UIControlStateNormal];
    [button3 addTarget:self action:@selector(clean:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button3];
    
    
    //添加.
    UIButton *button4=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button4 setFrame:CGRectMake(95, 345, 60, 60)];
    [button4 setTitle:@"." forState:UIControlStateNormal];
    [button4 addTarget:self action:@selector(shuzi:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button4];
    
    
    //后退
    UIButton *button5=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button5 setFrame:CGRectMake(160, 345, 60, 60)];
    [button5 setTitle:@"back" forState:UIControlStateNormal];
    [button5 addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button5];
    
    self.string=[[NSMutableString alloc]init];//初始化可变字符串,分配内存
    self.str = [[NSString alloc]init];
    // Do any additional setup after loading the view, typically from a nib.
}


//0-9方法
- (void)shuzi:(id)sender
{
    
    [self.string appendString:[sender currentTitle]];      //数字连续输入
    self.label.text=[NSString stringWithString:_string];   //显示数值
    self.num1=[self.label.text doubleValue];               //保存输入的数值
    NSLog(@"self.num1  is  %f",self.num1);
    
}

//计算方法
-(void)go:(id)sender
{
    if ([self.str isEqualToString:@""])//当str里为空
    {
        self.num2=self.num1;
        NSLog(@"self.num2  is  %f",self.num2);
        self.label.text=[NSString stringWithString:_string];     //只要是符号就显示数值
        [self.string setString:@""];                             //字符串清零
        self.str=[sender currentTitle];                          //保存运算符为了作判断作何种运算
        NSLog(@"%@",_str);
        [self.string appendString:self.str];
        self.label.text=[NSString stringWithString:_string];     //显示数值
        [self.string setString:@""];          //字符串清零
    }
    else
    {
        //输出上次计算结果
        if ([self.str isEqualToString:@"+"])//之前的符号是+
        {
            [self.string setString:@""];//字符串清零
            self.num2+=self.num1;//num2是运算符号左边的数值,还是计算结果
            
            //输出上次结果后判断这次输入的是何符号
            if ([[sender currentTitle]isEqualToString:@"="])
            {
                NSLog(@"self.num2 is %f",self.num2);
                self.label.text=[NSString stringWithFormat:@"%f",self.num2];
                self.str=@"";
            }
            else if ([[sender currentTitle]isEqualToString:@"+"]||[[sender currentTitle]isEqualToString:@"-"]||[[sender currentTitle]isEqualToString:@"*"]||[[sender currentTitle]isEqualToString:@"/"])
            {
                NSLog(@"self.num2 is %f",self.num2);
                self.label.text=[NSString stringWithFormat:@"%f",self.num2];
                [self.string setString:@""];   //字符串清零
                self.str=[sender currentTitle];//保存运算符为了作判断作何种运算
                NSLog(@"%@",_str);
                [self.string appendString:self.str];
                [self.string setString:@""];//字符串清零
            }
        }
        
        else if ([self.str isEqualToString:@"-"])//之前的符号是-
        {
            [self.string setString:@""];//字符串清零
            self.num2-=self.num1;
            //输出上次结果后判断这次输入的是何符号
            if ([[sender currentTitle]isEqualToString:@"="])
            {
                NSLog(@"self.num2  is  %f",self.num2);
                self.label.text=[NSString stringWithFormat:@"%f",self.num2];
                self.str=@"";
            }
            else if ([[sender currentTitle]isEqualToString:@"+"]||[[sender currentTitle]isEqualToString:@"-"]||[[sender currentTitle]isEqualToString:@"*"]||[[sender currentTitle]isEqualToString:@"/"])
            {
                NSLog(@"self.num2  is  %f",self.num2);
                self.label.text=[NSString stringWithFormat:@"%f",self.num2];
                [self.string setString:@""];//字符串清零
                self.str=[sender currentTitle];//保存运算符为了作判断作何种运算
                NSLog(@"%@",_str);
                [self.string appendString:self.str];
                [self.string setString:@""];//字符串清零
            }
        }
        
        else if([self.str hasPrefix:@"*"])//之前的符号是*   hasPrefix:方法的功能是判断创建的字符串内容是否以某个字符开始
        {
            [self.string setString:@""];//字符串清零
            self.num2*=self.num1;
            //输出上次结果后判断这次输入的是何符号
            if ([[sender currentTitle] isEqualToString:@"="])
            {
                NSLog(@"self.num2 is %f",self.num2);
                self.label.text=[NSString stringWithFormat:@"%f",self.num2];
                self.str=@"";
            }
            else if ([[sender currentTitle]isEqualToString:@"+"]||[[sender currentTitle]isEqualToString:@"-"]||[[sender currentTitle]isEqualToString:@"*"]||[[sender currentTitle]isEqualToString:@"/"])
            {
                NSLog(@"self.num2 is %f",self.num2);
                self.label.text=[NSString stringWithFormat:@"%f",self.num2];
                [self.string setString:@""];          //字符串清零
                self.str=[sender currentTitle];       //保存运算符为了作判断作何种运算
                NSLog(@"%@",_str);
                [self.string appendString:self.str];  //在字符串后增加新的东西,[a appendString:]
                [self.string setString:@""];          //字符串清零
            }
        }
        
        else if ([self.str isEqualToString:@"/"])//之前的符号是/
        {
            [self.string setString:@""];//字符串清零
            self.num2/=self.num1;
            //判断输出上次结果后判断这次输入的是何符号
            if ([[sender currentTitle]isEqualToString:@"="])
            {
                NSLog(@"self.num2  is  %f",self.num2);
                self.label.text=[NSString stringWithFormat:@"%f",self.num2];
                self.str=@"";
            }
            else if ([[sender currentTitle]isEqualToString:@"+"]||[[sender currentTitle]isEqualToString:@"-"]||[[sender currentTitle]isEqualToString:@"*"]||[[sender currentTitle]isEqualToString:@"/"])
            {
                NSLog(@"self.num2  is  %f",self.num2);
                self.label.text=[NSString stringWithFormat:@"%f",self.num2];
                [self.string setString:@""];//字符串清零
                self.str=[sender currentTitle];//保存运算符为了作判断作何种运算
                NSLog(@"%@",_str);
                [self.string appendString:self.str];
                [self.string setString:@""];//字符串清零
            }
            
        }
    }
}


//当按下清除建时,所有数据清零
-(void)clean:(id)sender
{
    [self.string setString:@""];//清空字符
    self.num1=0;
    self.num2=0;
    self.label.text=@"0";//保证下次输入时清零
    
}

//返回键
-(void)back:(id)sender
{
    if (![self.label.text isEqualToString:@""])//判断不是空
    {
        [self.string deleteCharactersInRange:NSMakeRange
         ([self.string length]-1,1)];//删除最后一个字符
        self.label.text=[NSString stringWithString:_string];//显示结果
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

 

img_e00999465d1c2c1b02df587a3ec9c13d.jpg
微信公众号: 猿人谷
如果您认为阅读这篇博客让您有些收获,不妨点击一下右下角的【推荐】
如果您希望与我交流互动,欢迎关注微信公众号
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。

目录
相关文章
|
3月前
|
编解码 iOS开发
IOS上架APP Store时预览图尺寸
IOS上架APP Store时预览图尺寸
639 3
|
3月前
|
iOS开发
App备案与iOS云管理式证书 ,公钥及证书SHA-1指纹的获取方法
App备案与iOS云管理式证书 ,公钥及证书SHA-1指纹的获取方法
185 0
App备案与iOS云管理式证书 ,公钥及证书SHA-1指纹的获取方法
|
3月前
|
开发工具 iOS开发
解决Flutter运行报错Could not run build/ios/iphoneos/Runner.app
解决Flutter运行报错Could not run build/ios/iphoneos/Runner.app
141 2
|
3月前
|
Android开发 iOS开发 C#
Xamarin:用C#打造跨平台移动应用的终极利器——从零开始构建你的第一个iOS与Android通用App,体验前所未有的高效与便捷开发之旅
【8月更文挑战第31天】Xamarin 是一个强大的框架,允许开发者使用单一的 C# 代码库构建高性能的原生移动应用,支持 iOS、Android 和 Windows 平台。作为微软的一部分,Xamarin 充分利用了 .NET 框架的强大功能,提供了丰富的 API 和工具集,简化了跨平台移动应用开发。本文通过一个简单的示例应用介绍了如何使用 Xamarin.Forms 快速创建跨平台应用,包括设置开发环境、定义用户界面和实现按钮点击事件处理逻辑。这个示例展示了 Xamarin.Forms 的基本功能,帮助开发者提高开发效率并实现一致的用户体验。
151 0
|
3月前
|
iOS开发
解决IOS上架App Store后显示语言为英文的问题
解决IOS上架App Store后显示语言为英文的问题
84 0
|
6月前
如何解决iOS16系统app首次启动总是弹出允许粘贴提示框问题
如何解决iOS16系统app首次启动总是弹出允许粘贴提示框问题
79 0
如何解决iOS16系统app首次启动总是弹出允许粘贴提示框问题
|
6月前
|
大数据 测试技术 数据库
ios app性能分析
ios app性能分析
48 2
|
6月前
|
移动开发 网络协议 Linux
We discovered one or more bugs in your app when reviewed on iPhone and iPad running iOS 14.1
We discovered one or more bugs in your app when reviewed on iPhone and iPad running iOS 14.1
63 0
|
6月前
|
定位技术 开发工具 iOS开发
ios9定位服务的app进入后台三分钟收不到经纬度,应用被挂起问题及解决方案
ios9定位服务的app进入后台三分钟收不到经纬度,应用被挂起问题及解决方案
59 0
|
6月前
|
iOS开发 网络架构 UED
ios app的分类与本质,感想
ios app的分类与本质,感想
69 0

热门文章

最新文章