'NSInternalInconsistencyException',原因:'无法在捆绑中加载NIB:'Xcode中的NSBundle

'NSInternalInconsistencyException',原因:'无法在捆绑中加载NIB:'Xcode中的NSBundle

问题描述:

我在写一个iOS应用程序,它实现了Google Cloud Messaging'NSInternalInconsistencyException',原因:'无法在捆绑中加载NIB:'Xcode中的NSBundle

我想收到授权令牌并将其打印在屏幕上。

我安装了所有必需的东西,并且我在YouTube上的教程之后编写了代码。

这是我的代码:

AppDelegate.h

#import <UIKit/UIKit.h> 

@class ViewController; 

@interface AppDelegate : UIResponder <UIApplicationDelegate>; 

@property (strong, nonatomic) UIWindow *window; 

@property (strong, nonatomic) ViewController *viewController; 

@property (nonatomic) NSString *getton; 

@end 

AppDelegate.m

#import "AppDelegate.h" 
#import "ViewController.h" 
#import "GoogleCloudMessaging.h" 

@implementation AppDelegate 

@synthesize window = _window; 
@synthesize viewController = _viewController; 
@synthesize getton; 

- (void) dealloc { 
    [_window release]; 
    [_viewController release]; 
    [super dealloc]; 
} 

- (BOOL)application:(UIApplication *)application 
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    self.window = [[[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]] autorelease]; 
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; 
    [[UIApplication sharedApplication] registerForRemoteNotifications]; 
    self.viewController = [[[ViewController alloc] initWithNibName:@"LaunchScreen.storyboard" bundle:nil] autorelease]; 
    self.window.rootViewController = self.viewController; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 
    [self registerDeviceToken: deviceToken]; 
} 

- (void) registerDeviceToken:(NSData *)deviceToken { 
    NSLog(@"Device Token: %@", deviceToken); 
    NSMutableString *string=[[NSMutableString alloc] init]; 
    int length=[deviceToken length]; 
    char const *bytes=[deviceToken bytes]; 
    for (int i=0; i<length; i++) { 
     [string appendString:[NSString stringWithFormat:@"%02.2hhx",bytes[i]]]; 
    } 
    NSLog(@"%@",string); 
    [self performSelectorInBackground:@selector(connectionWebRegister:)withObject:string]; 
    [string release]; 
} 

-(void) connectionWebRegister:(NSString *) deviceTokenString { 
    NSAutoreleasePool *pool = [NSAutoreleasePool new]; 
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://serviceProvider/registerTokenId?tokenId=%@&app=",deviceTokenString]]; 
    NSLog(@"APNS URL : %@",url); 
    NSData * res = [NSData dataWithContentsOfURL:url]; 
    getton=deviceTokenString; 
    if (res!=nil) { 
     NSString *response = [[NSString alloc] initWithBytes: [res bytes] lenght:[res length] encoding: NSUTF8StringEncoding]; 
     NSLog(@"%@", response); 
     [response release]; 
    } 
    [pool drain]; 
} 

- (void)application:(UIApplication *)app didReceiveRemoteNotification:(NSDictionary *)userInfo 
{ 
    NSLog(@"test"); 
    NSMutableDictionary * test = [userInfo objectForKey:@"aps"]; 
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"MESSAGE" 
                message:[test objectForKey:@"alert"] 
                delegate:nil 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
}  
@end 

ViewController.h和ViewController.m都是空的。

但是模拟器启动时,它崩溃,出现此错误:

<'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle' > with an < Thread 1: signal SIGABRT > error in Main.m .

我已经搜索互联网上的很多解决这个问题,但我没能解决它。

那么,有没有人可以帮助我?谢谢!然后

您应该使用

+0

你在主包项目有'LaunchScreen.storyboard' ? – Lion

+0

您不能像使用NIB文件一样使用故事板文件。您需要获取对故事板的引用,然后使用'instantiateViewControllerWithIdentifier' – Paulw11

如果你在问题中提到的名字故事情节存在,

UIStoryboard *storyboardobj=[UIStoryboard storyboardWithName:@"LaunchScreen" bundle:nil]; 

self.viewController = [storyboardobj instantiateInitialViewController]; 

,而不是

self.viewController = [[[ViewController alloc] initWithNibName:@"LaunchScreen.storyboard" bundle:nil] autorelease]; 
+0

谢谢,我解决了问题! – MatteoRK22

+0

不客气.... :) – Lion

+0

那么,是否比Objective-C更快捷?你呢? – MatteoRK22