In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
IOS how to implement a small calculator, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.
First of all, two controls are used to edit the calculator, the Label control and the Button control, the Label control is used to display the results, and Button is the corresponding key. I divided the calculator keys into three types: numButton,caculatorButton and clearButton. NumButton mainly has the numbers 0 to 9 and decimal point, caculatorButton has plus sign, minus sign, multiplication sign, division sign, equal sign. ClearButton has a clear key. So there are three ways. First of all, the control is connected, Label is connected to properties, and Button is connected to methods.
The figure of the calculator is as follows:
The specific code is as follows
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 super viewDidLoad 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 {naughty; float; num3=num2 Title= [sender titleForState:UIControlStateNormal]; if (masking 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 {natives; title= [sender titleForState:UIControlStateNormal]; num1= [[NSMutableString alloc] initWithString:title]; if (naughty 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, this is the answer to the question about how to implement a small calculator in iOS. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.