所以我创建了一个社交媒体应用程序的应用程序。我正在使用教程发现here作为跳板,因为我仍然试图围绕核心数据包装我的大脑。我偏离了教程,增加了一个注册按钮,让用户访问一个新的视图控制器,并创建一个.h和一个.m文件,并将新成员屏幕设置为引用.h和.m文件。他们建立了.H如下:Coredata帮手
#import <UIKit/UIKit.h>
@interface NewMemberViewController : UIViewController
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (strong, nonatomic) IBOutlet UITextField *nameTF;
@property (strong, nonatomic) IBOutlet UITextField *ageTF;
@property (strong, nonatomic) IBOutlet UITextField *usernameTF;
@property (strong, nonatomic) IBOutlet UITextField *passwordTF;
- (IBAction)alreadyMember:(id)sender;
- (IBAction)checkAndLogin:(id)sender;
@end
,为.M:
#import "NewMemberViewController.h"
#import "CoreDataHelper.h"
@interface NewMemberViewController()
@end
@implementation NewMemberViewController
@synthesize usernameTF, ageTF, passwordTF, nameTF, managedObjectContext;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//If the user is already a member simply dismiss the VC
- (IBAction)alreadyMember:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
//When done editing keyboard
- (IBAction)checkAndLogin:(id)sender {
managedObjectContext =self.managedObjectContext;
//Textfield Reference
UITextField *tf = (UITextField *)sender;
//Check tag numbers If its equal to 1 or 2(nameTF or ageTF) then
if (tf.tag==1||tf.tag==2)
{
[sender resignFirstResponder];
NSLog(@"This is working");
}
//If its equal to 3 then this means the username text field is active
else if (tf.tag == 3)
{
[sender resignFirstResponder];
//do a quick search to see if username is availible
NSPredicate *pred = [NSPredicate predicateWithFormat:@"(username == %@)", [usernameTF text]];
//Run the query to check if user exists
if([CoreDataHelper countForEntity:@"Users" withPredicate:pred andContext:managedObjectContext] > 0)
{
//we found a user
NSLog(@"oh no...");
}
}
}
@end
有了,如果我跑我的应用程序并进入注册画面和测试,看看这样说如果用户存在,通过管理输入,因为它已经存在,我得到以下错误在Xcode:
画布[779:11603] *的WebKit丢弃在web视图未捕获的异常:shouldInsertText :substitutionDOMRange:givenAction:delegate:+ entityForName:nil不是合法的NSManagedObjectContext参数搜索实体名称'用户'
这是什么意思?为什么它是由什么引起的?
为了这个工作,我需要导入.h文件中的任何东西? – joeBustamante
想说谢谢。我将它添加到了我的“checkAndLogin”动作的顶部,然后运行它并且我的应用程序正常运行!再次感谢。 – joeBustamante
你好! – RFG