I am very new to Objective-C . I am trying to send an object of type User(made with core data) from SingupViewController to its delegate ViewController. However I get this error that you get from what I've read because I am double instantiating memory for the same object ? . The error is as follows :
[User managedObjectModel]: unrecognized selector sent to instance 0x10ca90560
2015-03-14 03:44:49.613 Honk2[302:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[User managedObjectModel]: unrecognized selector sent to instance 0x10ca90560'
The code for SingupViewController.h
is :
#import <UIKit/UIKit.h>
#import "User.h"
@protocol SingupDelegate <NSObject>
- (void)signUp;
- (void)cancelPressDel;
- (void)savePress:(User *)addUser;
@end
@interface SingupViewController : UIViewController
@property (nonatomic, weak) id<SingupDelegate> delegate;
@end
The code for SingupViewController.m
is:
import "SingupViewController.h"
#import "UIViewController+MJPopupViewController.h"
#import "UIViewController+MJPopupViewController.h"
#import "LoginViewController.h"
#import "ViewController.h"
#import "CoreViewController.h"
@interface SingupViewController () <UITextFieldDelegate>
{
UILabel *titleApp;
UITextField *username;
UITextField *password;
UITextField *email;
UIButton *signUp;
UIButton *cancel;
}
@property(strong,nonatomic)User *thenewUser;
@end
@implementation SingupViewController
-(void)onSignUpPressed{
_thenewUser.name = username.text;
_thenewUser.email = email.text;
_thenewUser.password = password.text;
if([[self delegate] respondsToSelector:@selector(savePress:)]) {
[[self delegate] savePress:_thenewUser];
}
}
@end
The code for ViewController.h
is:
#import <UIKit/UIKit.h>
#import "CoreViewController.h"
#import "User.h"
@interface ViewController : CoreViewController <NSFetchedResultsControllerDelegate>
@end
And the code for ViewController.m
is:
#import "ViewController.h"
#import "UIViewController+MJPopupViewController.h"
#import "LoginViewController.h"
#import "SingupViewController.h"
#import "AppDelegate.h"
#import "CoreViewController.h"
@interface ViewController () <LoginDelegate, SingupDelegate>
{
LoginViewController *login;
SingupViewController *singup;
}
@property (nonatomic, strong) NSManagedObjectContext *managedObject;
@property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController;
@end
@implementation ViewController
-(void)savePress:(User *)addUser{
NSEntityDescription *description = [NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:[self managedObjectContext]];
NSManagedObject *newUser = [[NSManagedObject alloc] initWithEntity:description insertIntoManagedObjectContext:[self managedObjectContext]];
[newUser setValue:addUser.name forKey:@"name"];
[newUser setValue:addUser.email forKey:@"email"];
[newUser setValue:addUser.password forKey:@"password"];
NSError *error = nil;
if(![newUser.managedObjectContext save:&error]){
NSLog(@"invatam si noi sa codam?");
}
else{
NSLog(@"Hai ca am invatam sa codat");
}
[self dismissPopupViewControllerWithanimationType:MJPopupViewAnimationSlideTopBottom];
}
-(NSManagedObjectContext*)managedObjectContext{
return[(AppDelegate*)[[UIApplication sharedApplication]delegate]managedObjectContext];
}
The User.h
is the one given by Xcode :
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class Tweet;
@interface User : NSManagedObject
@property (nonatomic, retain) NSString * email;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * password;
@property (nonatomic, retain) NSSet *tweets;
@end
@interface User (CoreDataGeneratedAccessors)
- (void)addTweetsObject:(Tweet *)value;
- (void)removeTweetsObject:(Tweet *)value;
- (void)addTweets:(NSSet *)values;
- (void)removeTweets:(NSSet *)values;
@end
And this is the User.m
also not touched:
#import "User.h"
#import "Tweet.h"
@implementation User
@dynamic email;
@dynamic name;
@dynamic password;
@dynamic tweets;
@end
The error happens when I try to save the object. Can anyone tell me what I am allocating wrong ? Or what the fault is ?