iOS怎么实现简单计算器功能
发表于:2025-11-15 作者:千家信息网编辑
千家信息网最后更新 2025年11月15日,本篇文章给大家分享的是有关iOS怎么实现简单计算器功能,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。// ZYAppDelegate.
千家信息网最后更新 2025年11月15日iOS怎么实现简单计算器功能
本篇文章给大家分享的是有关iOS怎么实现简单计算器功能,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
// ZYAppDelegate.m// Calculator//// Created by mac on 15-7-30.// Copyright (c) 2015年 zhiyou. All rights reserved.// #import "ZYAppDelegate.h" @implementation ZYAppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; show=[[UITextField alloc] initWithFrame:CGRectMake(20, 30, 260, 30)]; show.borderStyle=UITextBorderStyleRoundedRect; show.text=@"0"; show.backgroundColor=[UIColor redColor]; [self.window addSubview:show]; UIButton *btn1=[UIButton buttonWithType:UIButtonTypeCustom]; btn1.frame=CGRectMake(20, 80, 40, 40); [btn1 setTitle:@"1" forState:UIControlStateNormal]; btn1.backgroundColor=[UIColor blueColor]; btn1.tag=1; [btn1 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside]; [self.window addSubview:btn1]; UIButton *btn2=[UIButton buttonWithType:UIButtonTypeCustom]; btn2.frame=CGRectMake(80, 80, 40, 40); [btn2 setTitle:@"2" forState:UIControlStateNormal]; btn2.backgroundColor=[UIColor blueColor]; btn2.tag=2; [btn2 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside]; [self.window addSubview:btn2]; UIButton *btn3=[UIButton buttonWithType:UIButtonTypeCustom]; btn3.frame=CGRectMake(140, 80, 40, 40); [btn3 setTitle:@"3" forState:UIControlStateNormal]; btn3.backgroundColor=[UIColor blueColor]; btn3.tag=3; [btn3 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside]; [self.window addSubview:btn3]; UIButton *btn4=[UIButton buttonWithType:UIButtonTypeCustom]; btn4.frame=CGRectMake(20, 140, 40, 40); [btn4 setTitle:@"4" forState:UIControlStateNormal]; btn4.backgroundColor=[UIColor blueColor]; btn4.tag=4; [btn4 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside]; [self.window addSubview:btn4]; UIButton *btn5=[UIButton buttonWithType:UIButtonTypeCustom]; btn5.frame=CGRectMake(80, 140, 40, 40); [btn5 setTitle:@"5" forState:UIControlStateNormal]; btn5.backgroundColor=[UIColor blueColor]; btn5.tag=5; [btn5 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside]; [self.window addSubview:btn5]; UIButton *btn6=[UIButton buttonWithType:UIButtonTypeCustom]; btn6.frame=CGRectMake(140, 140, 40, 40); [btn6 setTitle:@"6" forState:UIControlStateNormal]; btn6.backgroundColor=[UIColor blueColor]; btn6.tag=6; [btn6 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside]; [self.window addSubview:btn6]; UIButton *btn7=[UIButton buttonWithType:UIButtonTypeCustom]; btn7.frame=CGRectMake(20, 200, 40, 40); [btn7 setTitle:@"7" forState:UIControlStateNormal]; btn7.backgroundColor=[UIColor blueColor]; btn7.tag=7; [btn7 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside]; [self.window addSubview:btn7]; UIButton *btn8=[UIButton buttonWithType:UIButtonTypeCustom]; btn8.frame=CGRectMake(80, 200, 40, 40); [btn8 setTitle:@"8" forState:UIControlStateNormal]; btn8.backgroundColor=[UIColor blueColor]; btn8.tag=8; [btn8 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside]; [self.window addSubview:btn8]; UIButton *btn9=[UIButton buttonWithType:UIButtonTypeCustom]; btn9.frame=CGRectMake(140, 200, 40, 40); [btn9 setTitle:@"9" forState:UIControlStateNormal]; btn9.backgroundColor=[UIColor blueColor]; btn9.tag=9; [btn9 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside]; [self.window addSubview:btn9]; UIButton *btn0=[UIButton buttonWithType:UIButtonTypeCustom]; btn0.frame=CGRectMake(20, 260, 100, 40); [btn0 setTitle:@"0" forState:UIControlStateNormal]; btn0.backgroundColor=[UIColor blueColor]; btn0.tag=10; [btn0 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside]; [self.window addSubview:btn0]; UIButton *jia=[UIButton buttonWithType:UIButtonTypeCustom]; jia.frame=CGRectMake(200, 140, 40, 40); [jia setTitle:@"+" forState:UIControlStateNormal]; jia.backgroundColor=[UIColor blueColor]; jia.tag=100101; [jia addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; [self.window addSubview:jia]; UIButton *jian=[UIButton buttonWithType:UIButtonTypeCustom]; jian.frame=CGRectMake(200, 200, 40, 40); [jian setTitle:@"-" forState:UIControlStateNormal]; jian.backgroundColor=[UIColor blueColor]; jian.tag=100102; [jian addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; [self.window addSubview:jian]; UIButton *cheng=[UIButton buttonWithType:UIButtonTypeCustom]; cheng.frame=CGRectMake(200, 80, 40, 40); [cheng setTitle:@"*" forState:UIControlStateNormal]; cheng.backgroundColor=[UIColor blueColor]; cheng.tag=100103; [cheng addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; [self.window addSubview:cheng]; UIButton *chu=[UIButton buttonWithType:UIButtonTypeCustom]; chu.frame=CGRectMake(200, 260, 40, 40); [chu setTitle:@"/" forState:UIControlStateNormal]; chu.backgroundColor=[UIColor blueColor]; chu.tag=100104; [chu addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; [self.window addSubview:chu]; UIButton *deng=[UIButton buttonWithType:UIButtonTypeCustom]; deng.frame=CGRectMake(140, 260, 40, 40); [deng setTitle:@"=" forState:UIControlStateNormal]; deng.backgroundColor=[UIColor blueColor]; deng.tag=100105; [deng addTarget:self action:@selector(equal) forControlEvents:UIControlEventTouchUpInside]; [self.window addSubview:deng]; return YES;} -(void)onClick:(UIButton *)btn{ switch (btn.tag) { case 1: num1=@"1"; show.text=num1; a=[num1 intValue]; break; case 2: num1=@"2"; show.text=num1; a=[num1 intValue]; break; case 3: num1=@"3"; show.text=num1; a=[num1 intValue]; break; case 4: num1=@"4"; show.text=num1; a=[num1 intValue]; break; case 5: num1=@"5"; show.text=num1; a=[num1 intValue]; break; case 6: num1=@"6"; show.text=num1; a=[num1 intValue]; break; case 7: num1=@"7"; show.text=num1; a=[num1 intValue]; break; case 8: num1=@"8"; show.text=num1; a=[num1 intValue]; break; case 9: num1=@"9"; show.text=num1; a=[num1 intValue]; break; case 10: num1=@"0"; show.text=num1; a=[num1 intValue]; break; default: break; } }-(void)click:(UIButton *)btn{ switch (btn.tag) { case 100101: b=1; c=a; break; case 100102: b=2; c=a; break; case 100103: b=3; c=a; break; case 100104: b=4; c=a; break; } }-(void)equal{ if (b==1) { show.text=[NSString stringWithFormat:@"%d",(c+a)]; } if (b==2) { show.text=[NSString stringWithFormat:@"%d",(c-a)]; } if (b==3) { show.text=[NSString stringWithFormat:@"%d",(c*a)]; } if (b==4) { show.text=[NSString stringWithFormat:@"%d",(c/a)]; } }-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [self.window endEditing:YES];}- (void)applicationWillResignActive:(UIApplication *)application{ // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.} - (void)applicationDidEnterBackground:(UIApplication *)application{ // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.} - (void)applicationWillEnterForeground:(UIApplication *)application{ // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.} - (void)applicationDidBecomeActive:(UIApplication *)application{ // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.} - (void)applicationWillTerminate:(UIApplication *)application{ // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.} @end效果图:
以上就是iOS怎么实现简单计算器功能,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。
功能
计算器
更多
知识
篇文章
实用
就是
工作会
效果
效果图
文章
看吧
知识点
行业
详情
资讯
资讯频道
跟着
部分
频道
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
泰安定制软件开发公司
数据库索引原则
红蜻蜓数据库字典
青岛服务器
腾讯轻量云服务器恢复出厂
云服务器独立服务器
odx协议用什么软件开发的
hs22刀片服务器手册
密云区网络技术服务热线
sybase数据库所有结构
微微一笑倾城糜烂了服务器
烟草局网络安全宣传文章
杭州甄选网络技术有限公司
软件开发公司条例
重庆网络技术员招聘
微信公众号大数据库
富正网络技术有限公司怎么样
法网络安全面临的形势
数据库技术及应用实验九
dns服务器怎么显示是正常的
支付宝存储服务器安全吗
q绑16e数据库api
服务器磁盘转速哪里看
新建一个数据库账户
网络安全攻防演练视频解说词
有关网络安全小资料
娄底软件开发专业学校
软件开发能力等级评定
wpf数据库批量删除
濮阳软件开发方案