Вы находитесь на странице: 1из 8

CoreDataStudentAppDelegate.h // CoreDataStudent // // Created by mac on 13/09/11. // Copyright __MyCompanyName__ 2011. All rights reserved. // #import <UIKit/UIKit.h> #import <CoreData/CoreData.

h> @interface CoreDataStudentAppDelegate : NSObject <UIApplicationDelegate> { UIWindow *window; UINavigationController *navigationController; @private NSManagedObjectContext *managedObjectContext_; NSManagedObjectModel *managedObjectModel_; NSPersistentStoreCoordinator *persistentStoreCoordinator_; } @property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) IBOutlet UINavigationController *navigationController; @property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext; @property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel; @property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator; - (NSString *)applicationDocumentsDirectory;

@end ----------------------------------------------------------------

CoreDataStudentAppDelegate.m // CoreDataStudent // // Created by mac on 13/09/11. // Copyright __MyCompanyName__ 2011. All rights reserved. // #import "CoreDataStudentAppDelegate.h" #import "RootViewController.h" #import "DBStudent.h"

@implementation CoreDataStudentAppDelegate @synthesize window; @synthesize navigationController; #pragma mark #pragma mark Ap delete awakefromnib class

// RootViewController.h // CoreDataStudent // // Created by mac on 1file://localhost/Users/mac/Documents/CoreDataStudent/Classes/RootViewContro ller.xib3/09/11. // Copyright 2011 __MyCompanyName__. All rights reserved. // #import <UIKit/UIKit.h> #import "DBStudent.h" #import "Student.h" @interface RootViewController : UIViewController <UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>{ IBOutlet UITableView *tableStudents; IBOutlet UITextField *txtSno,*txtSname,*txtSMarks,*txtSCourse,*txtAddress,*txtmobile; DBStudent *dbSt; NSArray *arrStudents; Student *currentSelectedStudent; } -(IBAction)insertStudent; -(IBAction)showStudents; -(IBAction)updateStudent; -(IBAction)deleteStudent; @end ----------------------------------------------------------------RootViewController.m // CoreDataStudent // // Created by mac on 13/09/11.

// Copyright 2011 __MyCompanyName__. All rights reserved. // #import "RootViewController.h"

@implementation RootViewController - (BOOL)textFieldShouldReturn:(UITextField *)textField{ [textField resignFirstResponder]; return YES; } -(IBAction)insertStudent{ [dbSt insertStudent:[txtSno.text intValue] :txtSname.text :txtSCourse.text : [txtSMarks.text floatValue] :txtAddress.text :txtmobile.text]; [self showStudents]; } -(IBAction)showStudents{ arrStudents = [[dbSt getStudents]retain]; [tableStudents reloadData]; } -(IBAction)updateStudent{ if (currentSelectedStudent == nil) { return; } int sno = [txtSno.text intValue]; currentSelectedStudent.Sno = [NSNumber numberWithInt:sno]; currentSelectedStudent.Sname = txtSname.text; currentSelectedStudent.Course = txtSCourse.text; currentSelectedStudent.Address = txtAddress.text; currentSelectedStudent.Marks = [NSNumber numberWithFloat: [txtSMarks.text floatValue]]; currentSelectedStudent.MobileNo = txtmobile.text; [dbSt updateStudent]; [self showStudents]; } -(IBAction)deleteStudent{ if (currentSelectedStudent == nil) { return; } [dbSt deleteStudent:currentSelectedStudent]; [self showStudents]; }

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection: (NSInteger)section{ return [arrStudents count]; } // Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier: // Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls) - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSString *identifier = @"cell"; UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:identifier]; if (cell ==nil) { cell =[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier]autorelease]; } Student *st =[arrStudents objectAtIndex:indexPath.row]; cell.textLabel.text = st.Sname; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath{ currentSelectedStudent = [arrStudents objectAtIndex:indexPath.row]; txtSno.text = [NSString stringWithFormat:@"%@", currentSelectedStudent.Sno]; txtSname.text = currentSelectedStudent.Sname; txtSCourse.text = currentSelectedStudent.Course; txtSMarks.text = [NSString stringWithFormat:@"%@",currentSelectedStudent.Marks]; txtmobile.text = currentSelectedStudent.MobileNo; txtAddress.text = currentSelectedStudent.Address; } - (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ [dbSt deleteStudent:currentSelectedStudent]; [self showStudents]; }

/* // The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { // Custom initialization } return self; } */ // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. - (void)viewDidLoad { [super viewDidLoad]; dbSt = [[DBStudent alloc]init]; tableStudents.editing=YES; } /* // Override to allow orientations other than the default portrait orientation. - (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait); } */ - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (void)dealloc { [super dealloc]; [arrStudents release]; [tableStudents release]; [txtSno release]; [txtmobile release]; [txtSMarks release];

[txtAddress release]; [txtSCourse release]; [txtSname release]; [dbSt release]; } @end -------------------------------------------------------------------DBStudent.h // CoreDataStudent // // Created by mac on 13/09/11. // Copyright 2011 __MyCompanyName__. All rights reserved. // #import <Foundation/Foundation.h> #import "CoreDataStudentAppDelegate.h" #import "Student.h" @interface DBStudent : NSObject { CoreDataStudentAppDelegate *appDelegate; } -(BOOL)insertStudent:(int)sno :(NSString*)sname :(NSString*)Course : (float)Marks :(NSString*)Adress :(NSString*)mobile; -(NSArray*)getStudents; -(BOOL)updateStudent; -(BOOL)deleteStudent:(Student*)st; @end --------------------------------------------------------------DBStudent.m // CoreDataStudent // // Created by mac on 13/09/11. // Copyright 2011 __MyCompanyName__. All rights reserved. // #import "DBStudent.h" #import "Student.h" @implementation DBStudent -(id)init{ self = [super init]; if (self) { appDelegate = (CoreDataStudentAppDelegate*)[[UIApplication sharedApplication]delegate];

} return self; } -(BOOL)insertStudent:(int)sno :(NSString*)sname :(NSString*)Course : (float)Marks :(NSString*)Adress :(NSString*)mobile{ Student *st = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:appDelegate.managedObjectContext]; st.Sno = [NSNumber numberWithInt:sno]; st.Sname = sname; st.Course = Course; st.Marks = [NSNumber numberWithFloat:Marks]; st.Address = Adress; st.MobileNo = mobile; NSError *err; if ([appDelegate.managedObjectContext save:&err]==YES) { NSLog(@"inserted successfully"); return YES; } else { NSLog(@"insertion has problem %@",[err description]); return NO; } return NO; } -(NSArray*)getStudents{ NSEntityDescription *entity =[NSEntityDescription entityForName:@"Student" inManagedObjectContext:appDelegate.managedObjectContext]; NSFetchRequest *request= [[NSFetchRequest alloc]init]; [request setEntity:entity]; NSError *err; NSArray *arrStudents = [appDelegate.managedObjectContext executeFetchRequest:request error:&err]; return arrStudents; } -(BOOL)updateStudent{ NSError *err; if ([appDelegate.managedObjectContext save:&err]==YES) { NSLog(@"updated successfully"); return YES; } else { NSLog(@"updation has problem %@",[err description]); return NO;

} return NO; } -(BOOL)deleteStudent:(Student*)st{ [appDelegate.managedObjectContext deleteObject:st]; NSError *err; if ([appDelegate.managedObjectContext save:&err]==YES) { NSLog(@"deletion successfully"); return YES; } else { NSLog(@"deletion has problem %@",[err description]); return NO; } return NO; } @end

Вам также может понравиться