iOS怎么实现小型计算器
发表于:2024-09-22 作者:千家信息网编辑
千家信息网最后更新 2024年09月22日,iOS怎么实现小型计算器,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。首先呢,编辑这个计算器用到了两种控件,Label和Button
千家信息网最后更新 2024年09月22日iOS怎么实现小型计算器
iOS怎么实现小型计算器,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
首先呢,编辑这个计算器用到了两种控件,Label和Button控件,Label控件用于显示结果,而Button则是相应的键。我把计算器的键分为三种numButton,caculatorButton和clearButton。numButton主要有数字0到9还有小数点,caculatorButton有加号,减号,乘号,除号,等号。clearButton有清除键。所以总共有三种方法。首先先对控件进行连接,Label进行属性连接,Button进行方法连接。
计算器的图形如下:
具体的代码如下;
HHLDelegate.h
#import@class HHLViewController;@interface HHLAppDelegate : UIResponder @property (strong, nonatomic) UIWindow *window;@property (strong, nonatomic) HHLViewController *viewController; @end
HHLDelegate.m
#import "HHLAppDelegate.h" #import "HHLViewController.h" @implementation HHLAppDelegate - (void)dealloc{ [_window release]; [_viewController release]; [super dealloc];} - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch. self.viewController = [[[HHLViewController alloc] initWithNibName:@"HHLViewController" bundle:nil] autorelease]; self.window.rootViewController = self.viewController; self.viewController.view.backgroundColor=[[UIColor alloc]initWithRed:0.76 green:0.82 blue:0.94 alpha:0.8]; [self.window makeKeyAndVisible]; return 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
HHLViewController.h
#import@interface HHLViewController : UIViewController @property(retain,nonatomic)IBOutlet UILabel *label;@property(copy,nonatomic)NSString *title;@property(retain,nonatomic)NSMutableString *num1,*num2,*num3; - (IBAction)calculatorButton:(id)sender;- (IBAction)numButton:(id)sender;- (IBAction)clearButton:(id)sender;@end
HHLViewController.m
#import "HHLViewController.h" @interface HHLViewController () @end @implementation HHLViewController@synthesize label;@synthesize title;@synthesize num1,num2,num3; int m=0;int n=0; float y=0;float count=0;NSString *collect=@""; - (void)viewDidLoad{ [super viewDidLoad]; } - (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.} - (IBAction)calculatorButton:(id)sender{ n=0; m++; num3=num2; title=[sender titleForState:UIControlStateNormal]; if (m==1) { count=[num3 floatValue]; collect=title; } else{ if ([collect isEqualToString:@"+"]==1) { y=[num3 floatValue]; count=count+y; } if ([collect isEqualToString:@"-"]==1) { y=[num3 floatValue]; count=count-y; } if ([collect isEqualToString:@"*"]==1) { y=[num3 floatValue]; count=count*y; } if ([collect isEqualToString:@"/"]==1) { y=[num3 floatValue]; count=count/y; } label.text=[NSString stringWithFormat:@"%f",count]; collect=title; } } - (IBAction)numButton:(id)sender{ n++; title=[sender titleForState:UIControlStateNormal]; num1=[[NSMutableString alloc]initWithString:title]; if(n==1) { num2=num1; } else{ num2=[[NSMutableString alloc]initWithString:[num2 stringByAppendingString:num1]]; } label.text=num2; }- (IBAction)clearButton:(id)sender{label.text=@"";num1=num3=num2=[[NSMutableString alloc]initWithString:@""];collect=@"";count=0;m=0;n=0; }- (void)dealloc{ [num1 release]; [num2 release]; [num3 release]; [title release]; [label release]; [super dealloc];} @end
关于iOS怎么实现小型计算器问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。
计算器
控件
方法
问题
更多
帮助
解答
易行
简单易行
乘号
代码
内容
减号
图形
小伙
小伙伴
小数
小数点
属性
数字
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
查询整个数据库
深圳数据库数据恢复
服务器战争小说字数榜
建筑大师升级数据库
怎么打开服务器建立安全连接
会员卡数据库设计
打印机服务器网络扫描版
户籍数据库
女生做软件测试还是软件开发
软件开发集中在哪里
网络安全课活动感受
网络技术公司成本
软件开发下单
北京艾塔网络技术中心
校园网网络安全及服务器
最节能服务器cpu
gis软件开发工程师的前景
数据库安全管理语言类型
智能光网络技术特点
网络安全方面的小报
佛山易网互联网科技有限公司
证券数据库工程师
第三代网络安全体系核心
图形化编程软件开发
濡沫江湖无法获取服务器列表
网络安全学院专注人才培养
怎么修改t3数据库
上海欢聪互联网科技有限公司
计算机网络技术能修电脑吗
阿巴互联网科技