IOS计算器

.m文件
#import "AppDelegate.h"
#define KFrame(x,y,width,height) 
CGRectMake(x,y,width,height)

@implementation AppDelegate

- (void)dealloc
{
   [targetStr release];
   [_window 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.window.backgroundColor = [UIColor whiteColor];
   [self.window makeKeyAndVisible];
   
   targetStr = [[NSMutableString alloc] init];
   
   resultLabel =nil;
   resultLabel=[[UILabel alloc] initWithFrame:CGRectMake(10, 20, 300, 30)];
   [self.window addSubview:resultLabel];
   [resultLabel release];
   
   NSString *str = @"M+,M-,M*,M/,1,2,3,*,4,5,6,/,7,8,9,+,0,.,=,-";
   NSArray *titleArray = [str componentsSeparatedByString:@","];
   
   NSArray *selArray = [NSArray arrayWithObjects:
                        @"mPlus:",@"mMinus:",@"mMuti:",@"mRedu:",
                        @"num:",@"num:",@"num:",@"action:",
                        @"num:",@"num:",@"num:",@"action:",
                        @"num:",@"num:",@"num:",@"action:",
                        @"num:",@"num:",@"equal:",@"action:",
                        nil];
   
   for (int i = 0; i<5; i++)
   {
       for (int j = 0; j<4; j++)
       {
           UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
           btn.frame = CGRectMake(50+j*45+j*10, 60+i*45+i*10, 45, 45);
           [btn setTitle:[titleArray objectAtIndex:i*4+j]
                forState:0];
           
           NSString *selStr = [selArray objectAtIndex:i*4+j];
           // 重点关注SEL
           SEL sel = NSSelectorFromString(selStr);
           [btn addTarget:self
                   action:sel
         forControlEvents:UIControlEventTouchUpInside];
           
           [self.window addSubview:btn];
       }
   }
   
   
   
   return YES;
}

-(void)num:(UIButton *)sender
{
   //NSLog(@"%@",sender.currentTitle);
   [targetStr appendString:sender.currentTitle];
   
}

-(void)action:(UIButton *)sender
{
   if ([sender.currentTitle isEqualToString:@"*"])
   {
       op= @"*";
   }
   
   if ([sender.currentTitle isEqualToString:@"/"]) {
       op = @"/";
   }
   
   if ([sender.currentTitle isEqualToString:@"+"]) {
       op = @"+";
   }
   
   if ([sender.currentTitle isEqualToString:@"-"]) {
       op = @"-";
   }
   
   [targetStr appendString:sender.currentTitle];
}

-(void)equal:(UIButton *)sender
{
   //NSLog(@"target Str = %@",targetStr);
   [self calc:op];
}

-(void)calc:(NSString *)aOpType
{
   if ([aOpType isEqualToString:@"*"])
   {
       NSArray *nums=[targetStr componentsSeparatedByString:@"*"];
       int a = [[nums objectAtIndex:0] intValue];
       int b = [[nums objectAtIndex:1] intValue];
       int c = a * b;
       resultLabel.text = [NSString stringWithFormat:@"%d",c];
   }
}

-(void)mPlus:(id)sender
{
   [[NSUserDefaults standardUserDefaults] setObject:targetStr forKey:@"M"];
}

-(void)mMinus:(id)sender
{
   NSString *_str =[[NSUserDefaults standardUserDefaults] valueForKey:@"M"];
   NSLog(@"_str = %@",_str);
}
---------------------------------------------------------------------------------------------
.h文件
#import

@interface AppDelegate : UIResponder
{
   UILabel *resultLabel;
   NSMutableString *targetStr;
   NSString *op;
}
@property (strong, nonatomic) UIWindow *window;

@end

你可能感兴趣的:(IOS计算器)