Wednesday 18 December 2013

Simple arithmetic operation with validation Application in iphone

Operation.h

#import <UIKit/UIKit.h>

@interface DJ_15_OperationViewController : UIViewController <UITextFieldDelegate>

@property (nonatomic, retain) IBOutlet UITextField *t1;
@property (nonatomic, retain) IBOutlet UITextField *t2;
@property (nonatomic, retain) IBOutlet UITextField *t3;
@property (nonatomic, retain) IBOutlet UILabel *l1;
@property (nonatomic, retain) IBOutlet NSString *s1;
@property (nonatomic, retain) IBOutlet UIToolbar *u1;
          

-(IBAction)Add:(id)sender;
-(IBAction)Sub:(id)sender;
-(IBAction)Div:(id)sender;
-(IBAction)Mul:(id)sender;
-(IBAction)Clear:(id)sender;
-(IBAction)AboutUs:(id)sender;
-(IBAction)Toolbarclick:(id)sender;

@end

Operation.m


@synthesize t1;
@synthesize t2;
@synthesize t3;
@synthesize l1;
@synthesize s1;
@synthesize u1;
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    NSString *phoneRegex = @"^([0-9]+)?(\\.([0-9]{1,2})?)?$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];
    
    if([phoneTest evaluateWithObject:textField.text]==NO)
    {
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Validation" message:@"Only Number allow."
                                  delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:nil, nil];
       
        [alertView show];
        [textField becomeFirstResponder];
    }

   
   /* self.s1 = t1.text;
    NSString *nameString = s1;
    if ([nameString length] == 0)
    {
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Validation" message:@"Enter Fisrt No."
                                  delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:nil, nil];
       
        [alertView show]; 
        [textField resignFirstResponder];
    }*/
}

-(IBAction)Add:(id)sender;
{   
    if(t1.text.length != 0 && t2.text.length !=0)
    {   
    int result = [t1.text intValue] + [t2.text intValue];
    t3.text = [NSString stringWithFormat:@"%d", result];
    l1.text = @"Addition:"; 
    }
    else
    {
            UIAlertView *alertView = [[UIAlertView alloc]
                                      initWithTitle:@"Validation" message:@"PLEASE, Fill data."
                                      delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:nil, nil];
           
            [alertView show]; 
       
        if(t1.text.length ==0)
        {
                [t1 becomeFirstResponder];
        }
        else
        {
            [t2 becomeFirstResponder];
        }
    }
}

-(IBAction)Sub:(id)sender;
{
    if(t1.text.length != 0 && t2.text.length !=0)
    {   
    int result = [t1.text intValue] - [t2.text intValue];
    t3.text = [NSString stringWithFormat:@"%d", result];
    l1.text = @"Substraction:";
    }
    else
    {
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Validation" message:@"PLEASE, Fill data."
                                  delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:nil, nil];
       
        [alertView show]; 
        if(t1.text.length ==0)
        {
            [t1 becomeFirstResponder];
        }
        else
        {
            [t2 becomeFirstResponder];
        }
    }
   
}
-(IBAction)Div:(id)sender;
{
   
    if(t1.text.length != 0 && t2.text.length !=0)
    { 
       
    self.s1 = t1.text;   
    NSString *nameString = s1;
   
    self.s1 = t2.text;   
    NSString *nameString1 = s1; 
   
    if ([nameString length] != 0)       
    {     
        if ([nameString1 length] != 0)
        {    
            //NSLog(@"ada");
            if(t2.text.intValue == 0 )
            {
                UIAlertView *alertView = [[UIAlertView alloc]
                                          initWithTitle:@"Validation" message:@"Divide By 0 Not Possible."
                                          delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:nil, nil];
                
                [alertView show]; 
            }
            else
            {
                int result = [t1.text intValue] / [t2.text intValue];
                t3.text = [NSString stringWithFormat:@"%d", result];
                l1.text = @"Division:";
            }
        }   
    } 
    }
    else
    {
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Validation" message:@"PLEASE, Fill data."
                                  delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:nil, nil];
       
        [alertView show]; 
        if(t1.text.length ==0)
        {
            [t1 becomeFirstResponder];
        }
        else
        {
            [t2 becomeFirstResponder];
        }
    }
   
}
-(IBAction)Mul:(id)sender;
{
    if(t1.text.length != 0 && t2.text.length !=0)
    { 
    int result = [t1.text intValue] * [t2.text intValue];
    t3.text = [NSString stringWithFormat:@"%d", result];
    l1.text = @"Multiplication:";
    }
    else
    {
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Validation" message:@"PLEASE, Fill data."
                                  delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:nil, nil];
       
        [alertView show]; 
        if(t1.text.length ==0)
        {
            [t1 becomeFirstResponder];
        }
        else
        {
            [t2 becomeFirstResponder];
        }
    }
}
-(IBAction)Clear:(id)sender;
{
    UIButton *b = (UIButton *)sender;
    b.showsTouchWhenHighlighted = YES;
    t1.text = @"";
    t2.text = @"";
    t3.text = @"";
    l1.text = @"Ans";
}

- (void)viewDidLoad
{
    [super viewDidLoad];        
   
  /*  [super viewDidLoad];
   
    UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
    numberToolbar.barStyle = UIBarStyleBlackTranslucent;
    numberToolbar.items = [NSArray arrayWithObjects:
                           [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad)],
                           [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                           [[UIBarButtonItem alloc]initWithTitle:@"Apply" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)],
                           nil];
    [numberToolbar sizeToFit];
    t1.inputAccessoryView = numberToolbar;
   
    [super viewDidLoad];  
   
   
    UIToolbar* numberToolbar1 = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
    numberToolbar1.barStyle = UIBarStyleBlackTranslucent;
    numberToolbar1.items = [NSArray arrayWithObjects:
                           [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad1)],
                           [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                           [[UIBarButtonItem alloc]initWithTitle:@"Apply" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad1)],
                           nil];
    [numberToolbar1 sizeToFit];
   */
    t1.inputAccessoryView = u1;
    t2.inputAccessoryView = u1;
}

/*-(void)cancelNumberPad
{
    [t1 resignFirstResponder];
    t1.text = @"";    
}
-(void)doneWithNumberPad
{
    [t1 resignFirstResponder];
}
-(void)cancelNumberPad1{
  
    [t2 resignFirstResponder];
    t2.text = @"";
}
-(void)doneWithNumberPad1
{
    [t2 resignFirstResponder];
}
*/
-(IBAction)Toolbarclick:(id)sender
{
    [t1 resignFirstResponder];
    //t2.text = @"";
    [t2 resignFirstResponder];

}

-(IBAction)AboutUs:(id)sender
{
    UIAlertView *alertView = [[UIAlertView alloc]
                               initWithTitle:@"About US" message:@"Dharmendra Ambani"
                               delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
   
    [alertView show];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

No comments:

Post a Comment

Comment