启动时ios模拟器中的空白/黑屏

时间:2012-10-19 17:41:20

标签: ios xcode

我正在尝试将用户信息输入到coredata中,以便我可以将其发送到我的php并进行MySQL登录。然而,当我测试刚才的coredata部分时,我得到的只是一个没有xcode错误或错误报告的黑/空白屏幕(在自定义图像加载屏幕之后,应该是我的背景和我的按钮)。下面是我的代码,显然不包括storyboard和xcdatamodeld(实际存储核心数据输入)。我做错了什么?

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

1 个答案:

答案 0 :(得分:1)

原因是,您没有在窗口中添加任何viewcontrollers。

只需在其上添加您的logincontroller,

 - (BOOL)application:(UIApplication *)application
 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 {
      self.window = [[UIWindow alloc]
                initWithFrame:[[UIScreen mainScreen] bounds]];
      [self.window addSubView:viewController.view]; 
      // OR self.window.rootController = viewController;
      [self.window makeKeyAndVisible];
      return YES;

}