iOS實現(xiàn)小型計算器
作為一名初學(xué)者,編輯一款能夠在IOS操作系統(tǒng)上運行的計算器是一件很值得自豪的事情,網(wǎng)絡(luò)上雖然后很多相關(guān)的文章和代碼,功能也很強大但是我感覺相關(guān)的計算器比加復(fù)雜,晦澀難懂,所以我想通過這個小小的計算器,能夠幫到大家,如果有不完美的地方,還請大家多多批評指教。
首先呢,編輯這個計算器用到了兩種控件,Label和Button控件,Label控件用于顯示結(jié)果,而Button則是相應(yīng)的鍵。我把計算器的鍵分為三種numButton,caculatorButton和clearButton。numButton主要有數(shù)字0到9還有小數(shù)點,caculatorButton有加號,減號,乘號,除號,等號。clearButton有清除鍵。所以總共有三種方法。首先先對控件進(jìn)行連接,Label進(jìn)行屬性連接,Button進(jìn)行方法連接。
計算器的圖形如下:
具體的代碼如下;
HHLDelegate.h
#import <UIKit/UIKit.h> ? @class HHLViewController; @interface HHLAppDelegate : UIResponder <UIApplicationDelegate> @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 <UIKit/UIKit.h> ? @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
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS開發(fā)筆記--詳解UILabel的相關(guān)屬性設(shè)置
這篇文章主要介紹了iOS開發(fā)筆記--詳解UILabel的相關(guān)屬性設(shè)置,對初學(xué)者具有一定的參考價值,有需要的可以了解一下。2016-11-11微信JSSDK多圖片上傳并且解決IOS系統(tǒng)上傳一直加載的問題
這篇文章主要介紹了微信JSSDK多圖片上傳并且解決IOS系統(tǒng)上傳一直加載的問題的相關(guān)資料,需要的朋友可以參考下2016-03-03iOS當(dāng)多個網(wǎng)絡(luò)請求完成后執(zhí)行下一步的方法詳解
在多線程中,有時候我們會遇到一個界面同時有多個網(wǎng)絡(luò)請求(比如a,b,c,d四個網(wǎng)絡(luò)請求),在這四個個請求結(jié)束后,在請求到數(shù)據(jù)去做其他操作(UI更新等),下面這篇文章主要給大家介紹了關(guān)于iOS當(dāng)多個網(wǎng)絡(luò)請求完成后執(zhí)行下一步的相關(guān)資料,需要的朋友可以參考下。2017-12-12IOS中使用UIWebView 加載網(wǎng)頁、文件、 html的方法
UIWebView 是用來加載加載網(wǎng)頁數(shù)據(jù)的一個框,接下來通過本文給大家介紹IOS中使用UIWebView 加載網(wǎng)頁、文件、 html的方法,對本文詳情感興趣的朋友一起學(xué)習(xí)吧2016-02-02