Friday, August 2, 2013

Core Data Tute 3( Two Tables)



Student is a Undergraduate


Student is a Undergraduate
Step 1

Plz follow the Previous tutorials....



Step 2

Create a project like previous project then start with that project..

In previous project we add Student table , for that add another table call UnderGrad....





Step 3

click the "underGrad" entity and the set the parent entity 






Step 4
Design This Ui


Step 5

Run your Project
if do some editing for your entity.......  error  will occur when you run the project
because table values are change.....


if there any errors.... plz go to your Simulator and "Delete" your App

And run again....







Step 6

After you complete  above things .....
Change the code of Add,Search methods


---------------------Add Method ----------------

- (IBAction)add:(id)sender {

NSEntityDescription *entityDes=
[NSEntityDescription entityForName:@"UnderGrad" inManagedObjectContext:context];

NSManagedObject *newStudent =
[[NSManagedObject alloc]initWithEntity:entityDes insertIntoManagedObjectContext:context];

//student Attribute--------------------------------------------------------------------
[newStudent setValue:self.studentNameTextField.text forKey:@"studentName"];
[newStudent setValue:self.studentNoTextField.text forKey:@"studentNo"];

//extension to Student Attribute .... UnderGraduate Student----------------------------
[newStudent setValue:[NSNumber numberWithDouble:[self.undergraduateCourseFeeTextField.text doubleValue]] forKey:@"uFees"]; // convert to double
[newStudent setValue:self.undergraduateCourseNameTextField.text forKey:@"uCourseName"];

NSError *error;
[context save:&error];

self.displayLable.text=@"Student Added";

_studentNoTextField.text=@"";
_studentNameTextField.text=@"";
_undergraduateCourseFeeTextField.text=@"";
_undergraduateCourseNameTextField.text=@"";


}


-----------------------------Delete Method-----------------------------------

- (IBAction)deleteStudent:(id)sender{

NSEntityDescription *entityDes=
[NSEntityDescription entityForName:@"Student" inManagedObjectContext:context];

NSFetchRequest *request=[[NSFetchRequest alloc]init];
[request setEntity:entityDes];

//we use "==" to get exact match-------------------------------------------------------------
NSPredicate *predicate=[NSPredicate predicateWithFormat:@"studentNo == %@",self.studentNoTextField.text];

[request setPredicate:predicate];

NSError *error;
NSArray *machingData=[context executeFetchRequest:request error:&error];

if(machingData.count<=0)
{
self.displayLable.text=@"No Student Deleted";
}
else{

// int count=0;
for(NSManagedObject *obj in machingData){
[context deleteObject:obj];
//++count;
}

[context save:&error];
// self.displayLable.text=[NSString stringWithFormat:@"%d Student deleted : ",count];
self.displayLable.text=[NSString stringWithFormat:@"Student deleted"];

}

}


-----------------------Search Method-----------------------------------


- (IBAction)search:(id)sender {

NSEntityDescription *entityDes=
[NSEntityDescription entityForName:@"UnderGrad" inManagedObjectContext:context];

NSFetchRequest *request=[[NSFetchRequest alloc]init];
[request setEntity:entityDes];

//we use "==" to get exact match-------------------------------------------------------------

NSPredicate *predicate=[NSPredicate predicateWithFormat:@"studentNo == %@",self.studentNoTextField.text];

[request setPredicate:predicate];

NSError *error;
NSArray *machingData=[context executeFetchRequest:request error:&error];

if(machingData.count<=0)
{
self.displayLable.text=@"No Person Found";
}
else{

NSString *sno;//UnderGrad
NSString *sName;
NSString *courseName;
NSString *fee;
for(NSManagedObject *obj in machingData){
sno=[obj valueForKey:@"studentNo"];
sName=[obj valueForKey:@"studentName"];
fee =[obj valueForKey:@"uFees"];
courseName=[obj valueForKey:@"uCourseName"];

}

self.displayLable.text=[NSString stringWithFormat:@"Student No : %@ , Student Name : %@ , Course Fee : %@ , Course Name : %@ ",sno,sName,fee,courseName];

NSLog(@"display :studentNo %@ ,studentName %@ , Course fee %@ , Course Name : %@ ",sno,sName,fee,courseName);

}
}



Step 7
Run your Project
Enter the details
Type Student No and Click Search Button

















Tuesday, July 30, 2013

Core Data Tute 2 (Add / Delete / Search)

Core Data Tute 2 (Add / Delete / Search)




Step 1
Create a project like previous tutorial .... 


Step 2

Create Entity & Attribute   like this...



Step 3

Add new Class call "ViewController" Class with ".xib" like Previous tutorial 

and Design this UI in ViewControler.xib




Step 4
change the code in "ViewController.h" class . 


#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITextFieldDelegate>


@property (weak, nonatomic) IBOutlet UITextField *studentNoTextField;
@property (weak, nonatomic) IBOutlet UITextField *studentNameTextField;
@property (weak, nonatomic) IBOutlet UILabel *displayLable;


- (IBAction)add:(id)sender;
- (IBAction)search:(id)sender;
- (IBAction)deleteStudent:(id)sender;

@end
------------------------------------------------------------------------------------------------------------------------
Step 5

Change the code in  "ViewController.m" class. In here You have to Import AppDelegate class

#import "ViewController.h"
#import "AppDelegate.h"

@interface ViewController ()
{
NSManagedObjectContext *context;
}

@end

@implementation ViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

// for hide the key board-----------------------------------
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
return [textField resignFirstResponder];
}


- (IBAction)add:(id)sender {

NSEntityDescription *entityDes=
[NSEntityDescription entityForName:@"Student" inManagedObjectContext:context];

NSManagedObject *newStudent =
[[NSManagedObject alloc]initWithEntity:entityDes insertIntoManagedObjectContext:context];

[newStudent setValue:self.studentNameTextField.text forKey:@"studentName"];
[newStudent setValue:self.studentNoTextField.text forKey:@"studentNo"];

NSError *error;
[context save:&error];

self.displayLable.text=@"Student Added";

_studentNoTextField.text=@"";
_studentNameTextField.text=@"";

}


- (IBAction)deleteStudent:(id)sender{

NSEntityDescription *entityDes=
[NSEntityDescription entityForName:@"Student" inManagedObjectContext:context];

NSFetchRequest *request=[[NSFetchRequest alloc]init];
[request setEntity:entityDes];

//we use "==" to get exact match-------------------------------------------------

NSPredicate *predicate=[NSPredicate predicateWithFormat:@"studentNo == %@",self.studentNoTextField.text];
[request setPredicate:predicate];
NSError *error;


NSArray *machingData=[context executeFetchRequest:request error:&error];

if(machingData.count<=0)
{
self.displayLable.text=@"No Student Deleted";
}

else{

// int count=0;
for(NSManagedObject *obj in machingData){
[context deleteObject:obj];
//++count;
}

[context save:&error];
// self.displayLable.text=[NSString stringWithFormat:@"%d Student deleted : ",count];
self.displayLable.text=[NSString stringWithFormat:@"Student deleted"];
}

}

- (IBAction)search:(id)sender {

NSEntityDescription *entityDes=
[NSEntityDescription entityForName:@"Student" inManagedObjectContext:context];

NSFetchRequest *request=[[NSFetchRequest alloc]init];
[request setEntity:entityDes];

//we use "==" to get exact match-------------------------------------------------

NSPredicate *predicate=[NSPredicate predicateWithFormat:@"studentNo == %@",self.studentNoTextField.text];

[request setPredicate:predicate];

NSError *error;

NSArray *machingData=[context executeFetchRequest:request error:&error];

if(machingData.count<=0)
{
self.displayLable.text=@"No Person Found";
}
else{
NSString *sno;
NSString *sName;
for(NSManagedObject *obj in machingData){
sno=[obj valueForKey:@"studentNo"];
sName=[obj valueForKey:@"studentName"];
}

self.displayLable.text=[NSString stringWithFormat:@"Student No : %@ , Student Name : %@ ",sno,sName];
NSLog(@"display :studentNo %@ ,studentName %@ ",sno,sName);

}
}

- (void)viewDidLoad
{

AppDelegate *appDelegate=[[UIApplication sharedApplication]delegate];
context=[appDelegate managedObjectContext];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[[self studentNameTextField]setDelegate:self];
[[self studentNoTextField]setDelegate:self];

}

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

@end

------------------------------------------------------------------------------------------------------------------------------
Step 6

same like previous tutorial you have to set ViewController as a RootViewController in AppDelegate.
To do that....

"- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions"  method in AppDelegate.h


#import "ViewController.h"

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

// Override point for customization after application launch.
ViewController *rootViewController=
[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
[self.window setRootViewController:rootViewController];

self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}



2 . After Click Search Button
Step 7                                                                                     

Run your App

1






















3.After Click the add Button