Tuesday 17 December 2013

UISwitch with auto save data in ios

//
//  DJ_16_Utility1AppDelegate.h
//  DJ_16_Utility1
//
//  Created by Dharmendra on 2/14/13.
//  Copyright (c) 2013 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>
//#import "GlobalVariable.h"

@class DJ_16_Utility1MainViewController;

@interface DJ_16_Utility1AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) DJ_16_Utility1MainViewController *mainViewController;

@end

========================================================
//
//  DJ_16_Utility1AppDelegate.m
//  DJ_16_Utility1
//
//  Created by Dharmendra on 2/14/13.
//  Copyright (c) 2013 __MyCompanyName__. All rights reserved.
//

#import "DJ_16_Utility1AppDelegate.h"
#import "DJ_16_Utility1FlipsideViewController.h"
#import "DJ_16_Utility1MainViewController.h"

@implementation DJ_16_Utility1AppDelegate

@synthesize window = _window;
@synthesize mainViewController = _mainViewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.mainViewController = [[DJ_16_Utility1MainViewController alloc] initWithNibName:@"DJ_16_Utility1MainViewController" bundle:nil];
    self.window.rootViewController = self.mainViewController;
    [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
{

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    temp1 = [prefs boolForKey:@"key1"];
    temp2 = [prefs boolForKey:@"key2"];
    temp3 = [prefs boolForKey:@"key3"];
    temp4 = [prefs boolForKey:@"key4"];
    
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

======================================================
//
//  DJ_16_Utility1FlipsideViewController.h
//  DJ_16_Utility1
//
//  Created by Dharmendra on 2/14/13.
//  Copyright (c) 2013 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>
BOOL temp1,temp2,temp3,temp4;

@class DJ_16_Utility1MainViewController;
@class DJ_16_Utility1FlipsideViewController;

@protocol DJ_16_Utility1FlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish:(DJ_16_Utility1FlipsideViewController *)controller;
@end

@interface DJ_16_Utility1FlipsideViewController : UIViewController

@property (weak, nonatomic) id <DJ_16_Utility1FlipsideViewControllerDelegate> delegate;

@property (nonatomic, retain) IBOutlet UISwitch *s1;
@property (nonatomic, retain) IBOutlet UISwitch *s2;
@property (nonatomic, retain) IBOutlet UISwitch *s3;
@property (nonatomic, retain) IBOutlet UISwitch *s4;

- (IBAction)done:(id)sender;

- (IBAction) saveState;

-(IBAction) loadMySubview:(id)sender;


@end

===========================================================
//
//  DJ_16_Utility1FlipsideViewController.m
//  DJ_16_Utility1
//
//  Created by Dharmendra on 2/14/13.
//  Copyright (c) 2013 __MyCompanyName__. All rights reserved.
//

#import "DJ_16_Utility1FlipsideViewController.h"
#import "DJ_16_Utility1MainViewController.h"
//#import "GlobalVariable.h"



@implementation DJ_16_Utility1FlipsideViewController

@synthesize delegate = _delegate;
@synthesize s1;
@synthesize s2;
@synthesize s3;
@synthesize s4;


//-(IBAction)showSecond
//{
//    DJ_16_Utility1FlipsideViewController *controller = [[DJ_16_Utility1FlipsideViewController alloc] init];
//    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
//    
//    [self presentViewController:controller animated:YES completion:nil];
//}

- (void)viewDidLoad
{
  
    
    [super viewDidLoad];
    s1.on = temp1;
    s2.on = temp2;
    s3.on = temp3;
    s4.on = temp4;
}


- (IBAction) saveState
{
   // [[NSUserDefaults standardUSerDefaults] setBool:s1.on forKey:@"Key1"];
    //[NSUserDefaults standardUserDefaults] setBool:s1.on forKey:
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setBool:temp1 forKey:@"key1"];
    [prefs setBool:temp2 forKey:@"key2"];
    [prefs setBool:temp3 forKey:@"key3"];
    [prefs setBool:temp4 forKey:@"key4"];
    
    [prefs synchronize];
}


//- (IBAction)saveValue 
//{
//    NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
//    [preferences setBool:temp1 forKey:@"switchOnOff1"];
//    [preferences setBool:temp2 forKey:@"switchOnOff2"];
//    [preferences setBool:temp3 forKey:@"switchOnOff3"];
//    [preferences setBool:temp4 forKey:@"switchOnOff4"];
//    [preferences synchronize];
//    
//    


//NSString *value = @"ON";
//
//NSUserDefaults *userPreferences = [NSUserDefaults standardUserDefaults];
//if(!s1.on){
//    value = @"OFF";
//    [userPreferences setObject:value forKey:@"stateOfSwitch"];
//}
//[userPreferences setObject:value forKey:@"stateOfSwitch"];

    
       //store value to nsuserdefaulrs
    //    [[NSUserDefaults standardUserDefaults] setBool:temp1 forKey:@"switchStatus"];
    
        
        //get value from nsuserdefaulrs
      //  NSLog(@"%@",temp1);
        
        //[s1 setOn:[[NSUserDefaults standardUserDefaults] boolForKey:@"switchStatus"]];
    
//}

//- (BOOL)readValue 
//{
//    NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
//    return [preferences boolForKey:@"switchOnOff1"];
//     return [preferences boolForKey:@"switchOnOff2"];
//     return [preferences boolForKey:@"switchOnOff3"];
//     return [preferences boolForKey:@"switchOnOff4"];
//}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    temp1 = s1.on;
    temp2 = s2.on;
    temp3 = s3.on;
    temp4 = s4.on;


}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"viewWillAppear:");
    
}


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

#pragma mark - Actions

- (IBAction)done:(id)sender
{
       
    [self.delegate flipsideViewControllerDidFinish:self];
    temp1 = s1.on;
    temp2 = s2.on;
    temp3 = s3.on;
    temp4 = s4.on;
    
    [self saveState]; 


}

@end

=====================================================
//
//  DJ_16_Utility1MainViewController.h
//  DJ_16_Utility1
//
//  Created by Dharmendra on 2/14/13.
//  Copyright (c) 2013 __MyCompanyName__. All rights reserved.
//

#import "DJ_16_Utility1FlipsideViewController.h"
//# import "GlobalVariable.h"

//BOOL temp;

@interface DJ_16_Utility1MainViewController : UIViewController <DJ_16_Utility1FlipsideViewControllerDelegate,UITextFieldDelegate>

@property (nonatomic, retain) IBOutlet UITextField *t1;
@property (nonatomic, retain) IBOutlet UITextField *t2;

@property (nonatomic, retain) IBOutlet UILabel *Add1;
@property (nonatomic, retain) IBOutlet UILabel *Add2;
@property (nonatomic, retain) IBOutlet UILabel *Sub1;
@property (nonatomic, retain) IBOutlet UILabel *Sub2;
@property (nonatomic, retain) IBOutlet UILabel *Mul1;
@property (nonatomic, retain) IBOutlet UILabel *Mul2;
@property (nonatomic, retain) IBOutlet UILabel *Div1;
@property (nonatomic, retain) IBOutlet UILabel *Div2;

@property (nonatomic, retain) IBOutlet UIToolbar *u1;


- (IBAction)showInfo:(id)sender;
- (IBAction)btnResult:(id)sender;
- (IBAction)btnDone:(id)sender;
- (IBAction)About:(id)sender;


@end

=======================================================
//
//  DJ_16_Utility1MainViewController.m
//  DJ_16_Utility1
//
//  Created by Dharmendra on 2/14/13.
//  Copyright (c) 2013 __MyCompanyName__. All rights reserved.
//

#import "DJ_16_Utility1MainViewController.h"
#import "DJ_16_Utility1FlipsideViewController.h"
#import "GlobalVariable.h"

@implementation DJ_16_Utility1MainViewController

@synthesize t1;
@synthesize t2;
@synthesize Add1;
@synthesize Add2;
@synthesize Sub1;
@synthesize Sub2;
@synthesize Mul1;
@synthesize Mul2;
@synthesize Div1;
@synthesize Div2;
@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];
    }
}

- (IBAction)btnResult:(id)sender
{
    if (temp1)
    {
        Add1.hidden = false;
        Add2.hidden = false;
    }
    else 
    {
        Add1.hidden = true;
        Add2.hidden = true;
    }
    
    if (temp2)
    {
        Sub1.hidden = false;
        Sub2.hidden = false;
    }
    else 
    {
        Sub1.hidden = true;
        Sub2.hidden = true;
    }
    
    if (temp3)
    {
        Mul1.hidden = false;
        Mul2.hidden = false;
    }
    else
    {
        Mul1.hidden = true;
        Mul2.hidden = true;
    }
    if (temp4)
    {
        Div1.hidden = false;
        Div2.hidden = false;
    }
    else 
    {
        
        Div1.hidden = true;
        Div2.hidden = true;
    }
    

    if(t1.text.length != 0 && t2.text.length !=0)
    {  
        if(t2.text.intValue == 0)
        {
            UIAlertView *alertView = [[UIAlertView alloc] 
                                      initWithTitle:@"Validation" message:@"PLEASE,Divide By 0 Not Possible."
                                      delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:nil, nil];
            
            [alertView show]; 
        }
        else
        {
            if(t1.text.length != 0 && t2.text.length !=0)
            {    
             int result1 = [t1.text intValue] + [t2.text intValue];
                   Add2.text = [NSString stringWithFormat:@"%d", result1];
            }
            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];
                }
            }
        //int result1 = [t1.text intValue] + [t2.text intValue];
        int result2 = [t1.text intValue] - [t2.text intValue];
        int result3 = [t1.text intValue] * [t2.text intValue];
        int result4 = [t1.text intValue] / [t2.text intValue];
        
       // Add2.text = [NSString stringWithFormat:@"%d", result1];
        Sub2.text = [NSString stringWithFormat:@"%d", result2];
        Mul2.text = [NSString stringWithFormat:@"%d", result3];
        Div2.text = [NSString stringWithFormat:@"%d", result4];
        }
        
        //t3.text = [NSString stringWithFormat:@"%d", result];
        //l1.text = @"Addition:";  
    }
    //else
     if(t1.text.intValue == 0 )
    {
        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)btnDone:(id)sender
{
    [t1 resignFirstResponder];
    [t2 resignFirstResponder]; 
}

- (void)viewWillAppear:(BOOL)animated
{
    //[super viewWillAppear:animated];
    NSLog(@"viewWillAppear1:");
    


    

}

-(void)viewDidAppear:(BOOL)animated
{
        [self btnResult:0];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    t1.inputAccessoryView = u1; 
    t2.inputAccessoryView = u1;


    if (temp1)
    {
        Add1.hidden = false;
        Add2.hidden = false;
    }
    else {
        Add1.hidden = true;
        Add2.hidden = true;
    }
    
    if (temp2)
    {
        Sub1.hidden = false;
        Sub2.hidden = false;
    }
    else 
    {
        Sub1.hidden = true;
        Sub2.hidden = true;
    }
    
    if (temp3)
    {
        Mul1.hidden = false;
        Mul2.hidden = false;
    }
    else
    {
        Mul1.hidden = true;
        Mul2.hidden = true;
    }
    if (temp4)
    {
        Div1.hidden = false;
        Div2.hidden = false;
    }
    else 
    {
        
        Div1.hidden = true;
        Div2.hidden = true;
    }


    
}


- (void)viewDidUnload
{
    [super viewDidUnload];

}



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

#pragma mark - Flipside View

- (void)flipsideViewControllerDidFinish:(DJ_16_Utility1FlipsideViewController *)controller
{
    [self dismissModalViewControllerAnimated:YES];
  
}

- (IBAction)showInfo:(id)sender
{    
    DJ_16_Utility1FlipsideViewController *controller = [[DJ_16_Utility1FlipsideViewController alloc] initWithNibName:@"DJ_16_Utility1FlipsideViewController" bundle:nil];
    controller.delegate = self;
    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:controller animated:YES];
}
- (IBAction)About:(id)sender
{
    [GlobalVariable abc];
}

@end
=======================================================
//
//  GlobalVariable.h
//  DJ_16_Utility1
//
//  Created by Dharmendra on 2/14/13.
//  Copyright (c) 2013 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface GlobalVariable : NSObject

+(void)abc;
//-(void)xyz:(UITextField *)textField;

@end

=========================================
//
//  GlobalVariable.m
//  DJ_16_Utility1
//
//  Created by Dharmendra on 2/14/13.
//  Copyright (c) 2013 __MyCompanyName__. All rights reserved.
//

#import "GlobalVariable.h"
#import "DJ_16_Utility1FlipsideViewController.h"

@implementation GlobalVariable

+(void)abc
{
UIAlertView *alertView = [[UIAlertView alloc] 
                          initWithTitle:@"Validation" message:@"Super Method call."
                          delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:nil, nil];

[alertView show]; 
}

//-(void)xyz:(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];
//    }
//
//}
@end

No comments:

Post a Comment

Comment