Saturday, 21 February 2015

Calculator


1 )  First Of All Set Your StoryBoard Like This ...



2 ) Then Set Button Tag From The Property Window .... 


         Set    1   to   9   tag as it is Number 
         Set    0   to  10
         Set   +   to   11
         Set   -   to    12
         Set   *   to    13
         Set   /    to    14
         Set   %   to   15
         Set   =   to    16
         Set   C   to   17


3)   Write Code In ViewController.h File Like This ...



@interface ViewController : UIViewController<UITextFieldDelegate>
{
    int a;
    int b;
    int c;
}

@property (strong, nonatomic) IBOutlet UITextField *textfield;
- (BOOL)textFieldShouldReturn:(UITextField *)textField;   
@end


4)   Write Code In ViewController.m File Like This ...


@implementation ViewController
@synthesize textfield;
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
     // Dispose of any resources that can be recreated.
}
- (IBAction)genralmethod:(UIButton *)sender
{
    textfield.text=[textfield.text stringByAppendingString:sender.titleLabel.text];
}
- (IBAction)genralopmethod:(UIButton *)sender
{
     a=[textfield.text intValue];
    if (sender.tag==11)
    {
        c = sender.tag;
        textfield.text = @"";
      
    }
    else if (sender.tag==12)
    {
        c = sender.tag;
        textfield.text = @"";
        
    }
    else if (sender.tag==13)
    {
        c = sender.tag;
        textfield.text = @"";
    }
    else if (sender.tag==14)
    {
        c = sender.tag;
        textfield.text = @"";
    }
    else if (sender.tag==15)
    {
        c = sender.tag;
        textfield.text = @"";
    }
    else if (sender.tag == 17)
     {
        textfield.text = @"";
    }
   
}

- (IBAction)eq:(UIButton *)sender
{
    if (sender.tag==16)
   {
       switch (c) {
           case 11:
               b = a + [textfield.text intValue];
               textfield.text=[NSString stringWithFormat:@"%d",b];
               break;
               case 12:
               b = a - [textfield.text intValue];
               textfield.text=[NSString stringWithFormat:@"%d",b];
               break;
               case 13:
               b = a * [textfield.text intValue];
               textfield.text=[NSString stringWithFormat:@"%d",b];
               break;
               case 14:
               b = a / [textfield.text intValue];
               textfield.text=[NSString stringWithFormat:@"%d",b];
               break;
               case 15:
               b = a % [textfield.text intValue];
               textfield.text=[NSString stringWithFormat:@"%d",b];
               break;
               
           default:
               break;
       }
       
    }
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{

    [textfield resignFirstResponder];
    return YES;
}

@end

No comments:

Post a Comment