2013-10-02 32 views
0

所以我創建了一個社交媒體應用程序的應用程序。我正在使用教程發現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參數搜索實體名稱'用戶'

這是什麼意思?爲什麼它是由什麼引起的?

回答

0

您是否將上下文傳遞給視圖控制器?試用:

YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication]delegate]; 
yourVontext = [appDelegate managedObjectContext]; 

取前。

+0

爲了這個工作,我需要導入.h文件中的任何東西? – joeBustamante

+0

想說謝謝。我將它添加到了我的「checkAndLogin」動作的頂部,然後運行它並且我的應用程序正常運行!再次感謝。 – joeBustamante

+0

你好! – RFG

0

下方有問題。

managedObjectContext = self.managedObjectContext; 

self.managedObjectContext將返回零到managedObjectContext。除了self.managedObjectContext,您應該在應用程序委託中創建Core Data Stack(方法實現),然後將ManagedObjectContext的對象引用從AppDelegate傳遞到您的調用者類。

+0

我有一個在AppDelegate中設置的核心數據堆棧,但我將如何去傳遞對象引用? – joeBustamante

+0

@joeBustamante我想你已經得到了上述問題的答案。 –