DEV'S

CoreData Example

MainTableViewController.h

#import <UIKit/UIKit.h>

@interface MainTableViewController : UITableViewController

@property(nonatomic,retain) NSMutableArray *personData;
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
- (IBAction)addUserFormPressed:(id)sender;
- (IBAction)getCallBackFromFrom :(UIStoryboardSegue *)unwindSegue;
@end

MainTableViewController.m

#import "MainTableViewController.h"
#import "AppDelegate.h"
#import "UserForm.h"
#import "DetailViewController.h"

@interface MainTableViewController ()

@end

@implementation MainTableViewController

@synthesize personData;
@synthesize managedObjectContext;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    AppDelegate *app = [UIApplication sharedApplication].delegate;
    managedObjectContext = [app managedObjectContext];
     self.navigationItem.leftBarButtonItem = self.editButtonItem;
    
    /*
    
    UserForm *userForm = [NSEntityDescription insertNewObjectForEntityForName:@"UserForm" inManagedObjectContext:managedObjectContext];
    [userForm setUser_address:@"thoraipakkam"];
    [userForm setUser_city:@"Chennai"];
    [userForm setUser_full_name:@"Deva"];
    NSError *e;
    [managedObjectContext save:&e];
     
     */
    
    [self reloadAllData];
    
    
}

- (void)reloadAllData{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"UserForm"];
    NSError *error;
    //NSPredicate *predicate = [NSPredicate predicateWithFormat:@"user_city = %@",@"Mumbai"];
   // [request setPredicate:predicate];
    personData = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
    [self.tableView reloadData];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return [personData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    
    UserForm *userFrom = personData[indexPath.row];
    cell.textLabel.text = userFrom.user_full_name;
    // Configure the cell…
    
    return cell;
}

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        //[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        NSInteger selected = [[self.tableView indexPathForSelectedRow]row];
        UserForm *u = personData[selected];
        [managedObjectContext deleteObject:u];
        NSError *error;
        [managedObjectContext save:&error];
        [self reloadAllData];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    if ([segue.identifier isEqualToString:@"editFieldSegue"]){
        DetailViewController *detailCtrl = (DetailViewController *)segue.destinationViewController;
        detailCtrl.managedObjectContext = managedObjectContext;
        NSInteger selected = [[self.tableView indexPathForSelectedRow] row];
        
        detailCtrl.userFormForEdit = personData[selected];
        
    }else if ([segue.identifier isEqualToString:@"addFormSegue"]){
        DetailViewController *detailCtrl = (DetailViewController *)segue.destinationViewController;
        detailCtrl.managedObjectContext = managedObjectContext;
        
    }
}

- (IBAction)addUserFormPressed:(id)sender {
    
    [self performSegueWithIdentifier:@"addFormSegue" sender:nil];
}

-(IBAction)getCallBackFromFrom :(UIStoryboardSegue *)unwindSegue{
    
    [self reloadAllData];
    
    
}

@end

DetailViewController.h

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

@interface DetailViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *fullNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *addressTextField;
@property (weak, nonatomic) IBOutlet UITextField *cityTextField;

@property (nonatomic,retain)UserForm *userFormForEdit;
@property (nonatomic,retain)NSManagedObjectContext *managedObjectContext;

- (IBAction)savePressed:(id)sender;

@end

DetailViewController.h

#import "DetailViewController.h"

@interface DetailViewController ()

@end

@implementation DetailViewController

@synthesize userFormForEdit;
@synthesize fullNameTextField,addressTextField,cityTextField;
@synthesize managedObjectContext;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if (userFormForEdit){
        fullNameTextField.text = userFormForEdit.user_full_name;
        addressTextField.text = userFormForEdit.user_address;
        cityTextField.text = userFormForEdit.user_city;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)savePressed:(id)sender {
    NSError *error;
    if (userFormForEdit){
        //userFormForEdit = [[UserForm alloc]init];
        [userFormForEdit setUser_full_name:fullNameTextField.text];
        [userFormForEdit setUser_address:addressTextField.text];
        [userFormForEdit setUser_city:cityTextField.text];
    
        [managedObjectContext save:&error];
    }else{
        UserForm *userform = [NSEntityDescription insertNewObjectForEntityForName:@"UserForm" inManagedObjectContext:managedObjectContext];
        
        [userform setUser_full_name:fullNameTextField.text];
        [userform setUser_address:addressTextField.text];
        [userform setUser_city:cityTextField.text];
        
        [managedObjectContext save:&error];
        
    }
    
    [self performSegueWithIdentifier:@"backSegue" sender:nil];
}
@end

UserForm.h

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@interface UserForm : NSManagedObject

@property (nonatomic, retain) NSString * user_full_name;
@property (nonatomic, retain) NSString * user_address;
@property (nonatomic, retain) NSString * user_city;

@end

UserFrom.m

#import "UserForm.h"

@implementation UserForm

@dynamic user_full_name;
@dynamic user_address;
@dynamic user_city;

@end

iOS

2017-12-19 00:07:26
1) CoreData - UIDatePicker - Part2 - Final
2017-12-18 23:29:30
2) CoreData - UIDatePicker - Part1
2017-12-18 05:09:33
3) Shopping List App - Swift Part 2 - ActionSheet
2017-12-18 03:22:51
4) Shopping List App - Swift Part 1
2017-12-17 23:34:49
5) Swift - Introduction - WebService
2017-12-15 03:30:23
6) Shopping List IOS App - Part 2
2017-12-15 01:21:10
7) Shopping List IOS App - Part 1
2017-12-14 21:58:22
8) Shopping List IOS App - API Calls
2017-12-14 03:48:37
9) Delegate - Protocols - WebService
2017-12-13 04:28:23
10) NSMutableArray - NSMutableDictionary - TableViewController
2017-12-13 02:47:13
11) UITableViewController
2017-12-12 22:59:32
12) Segue Forward And Unwind
2016-02-27 03:20:22
13) GoogleMap Addon
2016-02-26 04:06:38
14) UILocalNotification With NSNotificationCenter
2016-02-25 03:51:07
15) CLLocationManager Example
2016-02-24 03:27:59
16) UIImagePickerController Using Camera And PhotoLibrary
2016-02-23 02:08:19
17) Multi Section UITableView - UIPickerView
2016-02-18 03:16:09
18) Hello World IOS App Using Swift
2016-02-17 03:10:24
19) Swift Programming Language - Playground
2016-02-15 02:06:55
20) TabBar Controller
2016-02-15 02:06:55
21) Understanding Auto Layout and Layout Constraints
2016-02-10 22:56:12
22) Shopping List App - Cloud - Part 3 - Final
2016-02-09 04:12:06
23) Shopping List App - Cloud - Part 2
2016-02-08 03:21:52
24) Shopping List App - Cloud - Part 1
2016-02-05 03:24:01
25) Protocol And Delegates With NSURLSession
2016-02-04 00:59:51
26) Webservice Using NSURLSession
2016-02-02 02:09:13
27) UIPickerView Addon - InputView And AccessoryView For UITextField
2016-02-02 02:07:12
28) Protocol And Delegates
2016-02-01 00:37:02
29) UIPickerView - Add Programmatically
2016-01-30 00:52:48
30) NSUserDefaults - UIAlertControler - Back Navigation Title Change
2016-01-29 02:01:06
31) CoreData - StudentsRecord App
2016-01-27 21:54:28
32) Manual Segue
2016-01-27 00:04:21
33) TableViewController Example
2016-01-25 02:08:16
34) NSArray And NSDictionary
2016-01-23 03:23:10
35) Reverse (Unwind) Segue
2016-01-22 00:56:28
36) ViewController - Segue - Messages
2014-12-22 05:29:28
37) WebService Example
2014-12-22 05:16:57
38) CoreData Example

© Vasudev.H / Email: tech3in@live.com