How to realize the small function of simple Calculator with iOS
This article shows you how iOS realizes the small function of simple calculator, the content is concise and easy to understand, it can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
SimpleCaculatorViewController.h
/ SimpleCaculatorViewController.h// SimpleCaculator//// Created by LI Junui on 14-2-12.Compact / Copyright (c) 2014 LEE JUNHUI. All rights reserved.// # import @ interface SimpleCaculatorViewController: UIViewController@property (weak, nonatomic) IBOutlet UILabel * displayScreen;- (IBAction) numberBtnClick: (UIButton *) sender;- (IBAction) clearDS: (UIButton *) sender;- (IBAction) caculate: (UIButton *) sender;- (IBAction) hint: (UIButton *) sender;- (IBAction) act: (UIButton *) sender;- (IBAction) clearBack: (UIButton *) sender; @ property (assign, nonatomic) BOOL isUserInputingNumber;@property (BOOL isUserInputingNumber;@property, assign) assign @ property (assign, nonatomic) int num2;@property (assign, nonatomic) int tagForAct; @ end
SimpleCaculatorViewController.m
/ SimpleCaculatorViewController.m// SimpleCaculator//// Created by LI Junui on 14-2-12.Compact / Copyright (c) 2014 LEE JUNHUI. All rights reserved.// # import "SimpleCaculatorViewController.h" @ interface SimpleCaculatorViewController () @ end @ implementation SimpleCaculatorViewController / / record number button Click event-(IBAction) numberBtnClick: (UIButton *) sender {if (self.isUserInputingNumber) {int re = [_ displayScreen.text intValue] * 10 + [sender.currentTitle intValue]; _ displayScreen.text = [NSString stringWithFormat:@ "% d", re];} else {[_ displayScreen setText:sender.currentTitle] _ isUserInputingNumber = YES;// because entering the program for the first time will enter a number, so the zero operation-(IBAction) clearDS: (UIButton *) sender {_ displayScreen.text = @ "0"; _ isUserInputingNumber = NO;// means that no more} / / results have been entered-(IBAction) caculate: (UIButton *) sender {int re = 0; _ num2 = [_ displayScreen.text intValue] Switch (_ tagForAct) {case 1: / / Additive re = _ num1 + _ num2; break; case 2: / / subtraction re = _ num1-_ num2; break; case 3: / / multiplicative re = _ num1 * _ num2; break Case 4: / / Division re = _ num1 / _ num2; break;} _ displayScreen.text = [NSString stringWithFormat:@ "=% d", re]; _ num1 = 0; _ num2 = 0 } / / Pop-up prompt dialog box-(IBAction) hint: (UIButton *) sender {UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@ "warm reminder" message:@ "this calculator produced by LJH" delegate:self cancelButtonTitle:@ "returns" otherButtonTitles: nil]; [alert show];} / / perform four operations-(IBAction) act: (UIButton *) sender {/ / 1. Get the number on _ displayScreen _ num1 = [_ displayScreen.text intValue]; _ displayScreen.text = sender.currentTitle; _ isUserInputingNumber = YES; switch (sender.tag) {case 1: / / addition _ tagForAct = 1; break; case 2: / / subtract _ tagForAct = 2; break Case 3: / / multiplication _ tagForAct = 3; break; case 4: / / Division _ tagForAct = 4; break;}} / / perform a delete operation-(IBAction) clearBack: (UIButton *) sender {int length = [_ displayScreen.text length]; int temp = [_ displayScreen.text intValue]; temp = temp/length } the above is how iOS implements the small functions of simple calculators. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.