Wednesday, 18 May 2016

Storing,Editing and Deleting NsUserDefaults data- Source Code

1) https://www.dropbox.com/s/xuw4p3olmpcg4st/DataStorenDel.zip?dl=0

2) Test Print) https://www.dropbox.com/s/6sf3m30ilbaic6e/TestPrint.zip?dl=0

Editing And Updating Stored Data in NSUserdefaults


//second view controller

-(void)edit:(id)sender{
    
    UIButton *tappedButton = (UIButton*)sender;
    q=tappedButton.tag;
    NSString *index=[NSString stringWithFormat:@"%d",q];
    [currentDefaults setObject:index forKey:@"edit"];
    [self.navigationController popViewControllerAnimated:YES];
    
}


// main ViewController

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    NSData *dataRepresentingSavedArray0 = [currentDefaults objectForKey:@"name"];
    NSMutableArray *oldSavedArray0 = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray0];
    nameArray = [[NSMutableArray alloc] initWithArray:oldSavedArray0];
    
    NSData *dataRepresentingSavedArray1 = [currentDefaults objectForKey:@"place"];
    NSMutableArray *oldSavedArray1 = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray1];
    placeArray = [[NSMutableArray alloc] initWithArray:oldSavedArray1];
    
    //if Editing Needs to be done on Stored Data
    
    if([currentDefaults objectForKey:@"edit"]){
        NSString *index=[currentDefaults valueForKey:@"edit"];
        p=[index intValue];
        
        myTextField1.text=[nameArray objectAtIndex:p];
        myTextField2.text=[placeArray objectAtIndex:p];
        
    }

}


- (IBAction)savedata:(id)sender{

    
    if([currentDefaults objectForKey:@"edit"]){
       
        [nameArray replaceObjectAtIndex:p withObject:myTextField1.text];
        [placeArray replaceObjectAtIndex:p withObject:myTextField2.text];
    }else{
    
    [nameArray addObject:myTextField1.text];
    [placeArray addObject:myTextField2.text];
    }
    
    [currentDefaults setObject:[NSKeyedArchiver archivedDataWithRootObject:nameArray] forKey:@"name"];
    [currentDefaults setObject:[NSKeyedArchiver archivedDataWithRootObject:placeArray] forKey:@"place"];
    myTextField1.text=@"";
    myTextField2.text=@"";
}


- (IBAction)listData:(id)sender{
    listView *anotherViewController = [[listView alloc] initWithNibName:@"listView" bundle:nil];
    
    [self.navigationController pushViewController:anotherViewController animated:YES];
    [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"edit"];
    myTextField1.text=@"";
    myTextField2.text=@"";

}

Deleting Data stored in NSUserdefaults (in TableView)

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
       
        [nameArray removeObjectAtIndex:q];
        [placeArray removeObjectAtIndex:q];
        
        
        [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:nameArray] forKey:@"name"];
        [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:placeArray] forKey:@"place"];
        
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        [myTavleView reloadData];
        [self.view setNeedsDisplay];
    }
    if (buttonIndex == 1) {
        NSLog(@"No Delete");
    }

}

Adding Buttons in a UITableView

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(250, 50, 49, 19);
    //[button setBackgroundColor:[UIColor clearColor]];
    UIImage *buttonImage = [UIImage imageNamed:@"DoneN.png"];
    [button setBackgroundImage:buttonImage forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(del:)forControlEvents:UIControlEventTouchUpInside];
    button.tag = indexPath.row;
    [cell.contentView addSubview:button];
    
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(250, 15, 50, 20);
    [button1 setBackgroundColor:[UIColor clearColor]];
    
    UIImage *buttonImage1 = [UIImage imageNamed:@"edit.png"];
    [button1 setBackgroundImage:buttonImage1 forState:UIControlStateNormal];
    
    
    [button1 addTarget:self action:@selector(edit:)forControlEvents:UIControlEventTouchUpInside];
    button1.tag = indexPath.row;
    [cell.contentView addSubview:button1];

    
    // Configure the cell.
    cell.textLabel.text= [nameArray objectAtIndex:indexPath.row];
    cell.detailTextLabel.text=[placeArray objectAtIndex:indexPath.row];
    
    
    return cell;

}



-(void)del:(id)sender
{
    
    UIButton *tappedButton = (UIButton*)sender;
    q=tappedButton.tag;
    
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Are you sure to delete this Data?" message:nil delegate:self cancelButtonTitle:@"ok" otherButtonTitles:@"Don't Allow", nil];
    [alert show];
    
    
}

-(void)edit:(id)sender{
    
    UIButton *tappedButton = (UIButton*)sender;
    q=tappedButton.tag;
    NSString *index=[NSString stringWithFormat:@"%d",q];
    [currentDefaults setObject:index forKey:@"edit"];
    [self.navigationController popViewControllerAnimated:YES];
    

}


Tuesday, 17 May 2016

Saving Data in NsuserDefaults and Retrieving

------------------------------------------------------------
//.h file
------------------------------------------------------------

#import <UIKit/UIKit.h>

@interface MasterViewController : UIViewController{
    IBOutlet UITextField *myTextField1;
     IBOutlet UITextField *myTextField2;
    
    NSMutableArray *nameArray;
    NSMutableArray *placeArray;
    
    NSUserDefaults *currentDefaults;
    
}

- (IBAction)savedata:(id)sender;
- (IBAction)listData:(id)sender;

@end

-------------------------------------------------------------
//.m file
-------------------------------------------------------------

#import "MasterViewController.h"
#import "listView.h"

@implementation MasterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [myTextField1 becomeFirstResponder];
    currentDefaults = [NSUserDefaults standardUserDefaults];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    NSData *dataRepresentingSavedArray0 = [currentDefaults objectForKey:@"name"];
    NSMutableArray *oldSavedArray0 = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray0];
    nameArray = [[NSMutableArray alloc] initWithArray:oldSavedArray0];
    
    NSData *dataRepresentingSavedArray1 = [currentDefaults objectForKey:@"place"];
    NSMutableArray *oldSavedArray1 = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray1];
    placeArray = [[NSMutableArray alloc] initWithArray:oldSavedArray1];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    // signup.enabled=YES;
    [myTextField1 resignFirstResponder];
    [myTextField2 resignFirstResponder];
    
    return YES;
}

- (IBAction)savedata:(id)sender{
    
    [nameArray addObject:myTextField1.text];
    [placeArray addObject:myTextField2.text];
    
    [currentDefaults setObject:[NSKeyedArchiver archivedDataWithRootObject:nameArray] forKey:@"name"];
    [currentDefaults setObject:[NSKeyedArchiver archivedDataWithRootObject:placeArray] forKey:@"place"];
    
}


- (IBAction)listData:(id)sender{
    listView *anotherViewController = [[listView alloc] initWithNibName:@"listView" bundle:nil];
    
    [self.navigationController pushViewController:anotherViewController animated:YES];

}
@end

------------------------------------------------
Retrieving in Another View Controller(listView)
------------------------------------------------
//.h file
-----------------------------------------------

#import <UIKit/UIKit.h>

@interface listView : UIViewController<UITableViewDelegate>{

    IBOutlet UITableView *myTavleView;
    
    NSMutableArray *nameArray;
    NSMutableArray *placeArray;
    NSUserDefaults *currentDefaults;
}
@end

-----------------------------------------------
//.m file
-----------------------------------------------

#import "listView.h"

@implementation listView

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

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    currentDefaults = [NSUserDefaults standardUserDefaults];
    
    NSData *dataRepresentingSavedArray = [currentDefaults objectForKey:@"name"];
    NSMutableArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray];
    nameArray = [[NSMutableArray alloc] initWithArray:oldSavedArray];
    
    NSData *dataRepresentingSavedArray0 = [currentDefaults objectForKey:@"place"];
    NSMutableArray *oldSavedArray0 = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray0];
    placeArray = [[NSMutableArray alloc] initWithArray:oldSavedArray0];
   
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 75.0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [nameArray count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    
    // Configure the cell.
    cell.textLabel.text= [nameArray objectAtIndex:indexPath.row];
    cell.detailTextLabel.text=[placeArray objectAtIndex:indexPath.row];
    
    
    return cell;
}
@end

------------------------------------------------------------------------------------




Different views for Different iPhone sizes

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
     UIStoryboard *storyboard = [self grabStoryboard];
    self.window.rootViewController = [storyboard instantiateInitialViewController];
    [self.window makeKeyAndVisible];

    return YES;
}
- (UIStoryboard *)grabStoryboard {
    
    // determine screen size
    int screenHeight = [UIScreen mainScreen].bounds.size.height;
    UIStoryboard *storyboard;
    
    switch (screenHeight) {
            
            // iPhone 5s
        case 568:
            storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            break;
            
            // iPhone 6
        case 667:
            storyboard = [UIStoryboard storyboardWithName:@"Main6" bundle:nil];
            break;
            
            // iPhone 6 Plus
        case 736:
            storyboard = [UIStoryboard storyboardWithName:@"Main6s" bundle:nil];
            break;
            
        default:
            // it's an iPad
            storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            break;
    }
    
    return storyboard;

}