1
我正在嘗試向coredata輸入用戶信息,以便我可以將它發送到我的php並執行MySQL登錄。然而,當我測試只是coredata部分時,我所得到的只是一個黑屏/空白屏幕,沒有xcode錯誤或錯誤報告(在自定義圖像加載屏幕後,應該有我的背景和我的按鈕)。下面是我的代碼,顯然排除故事板和xcdatamodeld(實際存儲核心數據輸入)。我做錯了什麼?啓動時在ios模擬器中顯示空白/黑屏
Appdelegate.h
#import <UIKit/UIKit.h>
@class LoginViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
LoginViewController *viewController;
}
@property (strong, nonatomic) IBOutlet LoginViewController *viewController;
@property (strong, nonatomic) UIWindow *window;
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (void)saveContext;
@end
Appdelegate.m
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize window = _window;
@synthesize managedObjectContext = __managedObjectContext;
@synthesize managedObjectModel = __managedObjectModel;
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator;
@synthesize viewController;
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc]
initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window makeKeyAndVisible];
return YES;
}
LoginViewController.h
#import <UIKit/UIKit.h>
@interface LoginViewController : UIViewController {
UITextField *username;
UITextField *password;
}
@property (strong, nonatomic) IBOutlet UITextField *username;
@property (strong, nonatomic) IBOutlet UITextField *password;
- (IBAction)saveData:(id)sender;
@end
LoginViewController.m
#import "LoginViewController.h"
#import "AppDelegate.h"
#import "Contacts.h"
@interface LoginViewController()
@end
@implementation LoginViewController
@synthesize username, password;
- (IBAction)saveData:(id)sender {
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSManagedObject *newContact;
newContact = [NSEntityDescription insertNewObjectForEntityForName:@"Contacts" inManagedObjectContext:context];
[newContact setValue:username.text forKey:@"username"];
[newContact setValue:password.text forKey:@"password"];
username.text = @"";
password.text = @"";
NSError *error;
[context save:&error];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
@end
Contacts.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface Contacts : NSManagedObject
@property (nonatomic, retain) NSString * username;
@property (nonatomic, retain) NSString * password;
@end
Contacts.m
#import "Contacts.h"
@implementation Contacts
@dynamic username;
@dynamic password;
@end
我欣賞的幫助,但它不喜歡.window。它一直說沒有找到該物業。我應該怎麼做才能「找到」它 – user1746156
我在編輯plist版本和歸檔構建後意外地點擊了刪除鍵。保持黑色的窗戶,花費2小時試圖找到它。 git diff向我展示了這個問題。哈哈謝謝。 +1 <3 – Kalle