你好,我只是創建一個樣本項目,我有登錄和註冊class.i創建一個註冊類與所有textfields和註冊成功與coredata.The問題是我需要註冊用戶登錄類,使其成功進入下一頁。我必須檢查用戶是否存在或不在我的Login類中。Coredata抓取概念
Signup.m
@implementation CoreSignup
@synthesize managesObjectContext=_managesObjectContext;
@synthesize managesObjectModel=_managesObjectModel;
@synthesize persistentStoreCoordinator=_persistentStoreCoordinator;
#pragma mark Insert Row In CoreData
-(void)insertSignUpList:(NSMutableDictionary *)details_Ary
{
BOOL isInserted=[self insertRowForSignUp:[details_Ary valueForKey:@"Firstname"] Lastname:[details_Ary valueForKey:@"Lastname"] password:[details_Ary valueForKey:@"Password"] emailid:[details_Ary valueForKey:@"Emailid"] phoneNo:[details_Ary valueForKey:@"Phoneno"] city:[details_Ary valueForKey:@"City"]];
if (isInserted)
{
//inserted Successfully
NSLog(@"inserted Successfully");
}
}
-(BOOL)insertRowForSignUp:(NSString *)firstName Lastname:(NSString *)LastName password:(NSString *)password emailid:(NSString *)emailid phoneNo:(NSString *)phoneNo city:(NSString *)city{
AppDelegate *appDelegate =[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context =
[appDelegate managedObjectContext];
SignUp *signUpInfo=[NSEntityDescription insertNewObjectForEntityForName:@"SignUp" inManagedObjectContext:context];
signUpInfo.firstName=firstName;
signUpInfo.lastName=LastName;
signUpInfo.password=password;
[email protected]([phoneNo intValue]);
signUpInfo.city=city;
signUpInfo.emailid=emailid;
NSError *error;
if (![context save:&error]) {
NSLog(@"Oops coudnt save");
return NO;
}
return YES;
}
-(NSMutableArray*)fetchAll{
AppDelegate *appDelegate =[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context =
[appDelegate managedObjectContext];
NSFetchRequest *request=[[NSFetchRequest alloc]init];
NSEntityDescription *entity1=[NSEntityDescription entityForName:@"SignUp" inManagedObjectContext:context];
[request setEntity:entity1];
NSArray *emptyArray=[self.managesObjectContext executeFetchRequest:request error:nil];
NSMutableArray *AllHistory=[NSMutableArray new];
for (SignUp*signUpHistory in emptyArray) {
NSMutableDictionary *tempDict=[[NSMutableDictionary alloc]init];
[tempDict setObject:[NSString stringWithFormat:@"%@",signUpHistory.firstName] forKey:@"firstname"];
[tempDict setObject:[NSString stringWithFormat:@"%@",signUpHistory.lastName] forKey:@"lastName"];
[tempDict setObject:[NSString stringWithFormat:@"%@",signUpHistory.password] forKey:@"password"];
[tempDict setObject:[NSString stringWithFormat:@"%@",signUpHistory.emailid] forKey:@"emailid"];
[tempDict setObject:[NSString stringWithFormat:@"%@",signUpHistory.city] forKey:@"city"];
[tempDict setObject:[NSString stringWithFormat:@"%@",signUpHistory.phoneno] forKey:@"phoneno"];
[AllHistory addObject:tempDict];
}
return AllHistory;
}
Login.m
@interface ViewController()<UITextFieldDelegate>
@end
@implementation ViewController
@synthesize JJuser;
@synthesize JJpassword;
@synthesize managesObjectContext=_managesObjectContext;
@synthesize managesObjectModel=_managesObjectModel;
@synthesize persistentStoreCoordinator=_persistentStoreCoordinator;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self jjtext];
[self jjpass];
}
-(void)jjtext{
JJuser.delegate=self;
[JJuser enableMaterialPlaceHolder:YES];
}
-(void)jjpass{
JJpassword.delegate=self;
[JJpassword enableMaterialPlaceHolder:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSMutableArray*)fetchLogin{
AppDelegate *appDelegate =[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context =
[appDelegate managedObjectContext];
NSFetchRequest *request=[[NSFetchRequest alloc]init];
NSEntityDescription *entity1=[NSEntityDescription entityForName:@"SignUp" inManagedObjectContext:context];
[request setEntity:entity1];
NSArray *emptyArray=[self.managesObjectContext executeFetchRequest:request error:nil];
NSMutableArray *AllHistory=[NSMutableArray new];
for (SignUp*signUpHistory in emptyArray) {
NSMutableDictionary *tempDict=[[NSMutableDictionary alloc]init];
[tempDict setObject:[NSString stringWithFormat:@"%@",signUpHistory.firstName] forKey:@"firstname"];
[tempDict setObject:[NSString stringWithFormat:@"%@",signUpHistory.lastName] forKey:@"lastName"];
[tempDict setObject:[NSString stringWithFormat:@"%@",signUpHistory.password] forKey:@"password"];
[tempDict setObject:[NSString stringWithFormat:@"%@",signUpHistory.emailid] forKey:@"emailid"];
[tempDict setObject:[NSString stringWithFormat:@"%@",signUpHistory.city] forKey:@"city"];
[tempDict setObject:[NSString stringWithFormat:@"%@",signUpHistory.phoneno] forKey:@"phoneno"];
[AllHistory addObject:tempDict];
}
return AllHistory;
}
- (IBAction)Login:(id)sender {
AppDelegate *appDelegate =[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context =
[appDelegate managedObjectContext];
NSFetchRequest *FindIt=[[NSFetchRequest alloc]init];
NSEntityDescription *entity1=[NSEntityDescription entityForName:@"SignUp" inManagedObjectContext:context];
[FindIt setEntity:entity1];
NSPredicate *pred = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"firstName = \"%@\"",FindIt]];
[FindIt setPredicate:pred];
NSUInteger count = [context countForFetchRequest:FindIt error:nil];
if (count == NSNotFound)
{
UIAlertView*alert=[[UIAlertView alloc]initWithTitle:@"Hello" message:@"No Such id exist" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
}else if (count == 1)
{
}
else{
}
}
- (IBAction)Signup:(id)sender
{
UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
SignUpViewController*SignUp = [board instantiateViewControllerWithIdentifier:@"SignUp"];
[self.navigationController pushViewController:SignUp animated:YES];
}
@end
您希望用戶在登錄屏幕註冊後登錄嗎? – channi
是的正確@ channi ..... – Arun