运行我的第一个iOS应用程序时获得了SIGABRT

时间:2014-04-16 12:33:52

标签: ios objective-c sigabrt

尝试使用Xcode 5.1编写我的第一个iPhone应用程序。这是我的 AppDelegate 代码

的一部分
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    self.viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"AddViewController"];
    self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.window setRootViewController:self.viewController];
    [self.window addSubview:self.navigationController.view];
    [self.window makeKeyAndVisible];

    return YES;
}

我有一个名为" Main.Storyboard "的故事板。使用导航控制器查看控制器

当我运行我的应用时,我得到SIGABRT error with NSInternalInconsistencyException exception

有人可以帮我解决这个错误吗?

感谢。

3 个答案:

答案 0 :(得分:2)

为什么要将子视图添加为UINavigationController.view,将根视图添加为view-controller?您的代码必须如下所示: -

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    self.viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"AddViewController"];
    self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.window setRootViewController:self.navigationController];

    [self.window makeKeyAndVisible];

    return YES;
}

并验证了您的viewcontroller的标识符设置是否正确,您在上面给出了。

答案 1 :(得分:0)

试试这个

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  UIStoryboard *storyBord = [UIStoryboard storyboardWithName:@"Main_iPad" bundle:[NSBundle  mainBundle]];
  self.window.rootViewController = [storyBord instantiateInitialViewController] ;
 [self.window makeKeyAndVisible];
 return YES;
}

答案 2 :(得分:0)

如果您使用故事板作为主界面,则无需设置任何代码。因此,您可以将UINavigationController添加到故事板中的场景中。然后使用连接检查器设置rootViewController。

查看视频以了解:link

代码如下所示:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    return YES;
}