【问题标题】:instantiateViewControllerWithIdentifier with storyboard用故事板实例化ViewControllerWithIdentifier
【发布时间】:2015-03-02 16:25:14
【问题描述】:

我有一个复杂的项目(不是我写的)。 我必须在情节提要的第一个屏幕之前添加一个新屏幕。 在 App Delegate 中,他们硬编码从情节提要中获取视图控制器。 我尝试重写此代码失败了。

原始代码(AppDelegate.m):

    UINavigationController *favoriteMealsNavigationController = [[tabBarController viewControllers] objectAtIndex:1];
    FavoriteMealsViewController *favoriteMealsViewController = [[favoriteMealsNavigationController viewControllers] objectAtIndex:0];
[favoriteMealsViewController setManagedObjectContext:[self managedObjectContext]];

我的尝试:

    FavoriteMealsViewController *favoriteMealsViewController = (FavoriteMealsViewController *)[self.mainStoryboard instantiateViewControllerWithIdentifier:@"mealsScene"];
//crashes here:
    [favoriteMealsViewController setManagedObjectContext:[self managedObjectContext]];

错误信息: [UINavigationController setManagedObjectContext:]:无法识别的选择器发送到实例 0x17d7bfe0 *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[UINavigationController setManagedObjectContext:]: 无法识别的选择器发送到实例

*favoriteMealsViewController 不为空 self.managedObjectContext 不为空

这是FavoriteMealsViewController.h中的代码

@interface FavoriteMealsViewController : UIViewController <NSFetchedResultsControllerDelegate, UIActionSheetDelegate, MealAddDelegate>

/** The managed object context associated with the Core Data Model. */
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;


@end

这是FavoriteMealsViewController.h中的相关代码:

@synthesize managedObjectContext = _managedObjectContext;

【问题讨论】:

  • 您在故事板中的视图称为“mealsScene”?你设置了这个名字?
  • 错误信息表明您正在实例化UINavigationController 而不是FavoriteMealsViewController

标签: ios objective-c xcode


【解决方案1】:

这意味着您尝试使用此标识符实例化 FavoriteMealsViewController:@"mealsScene" 实际上是在实例化 UINavigationController。转到您的故事板并确保您使用的 ID 用于 FavoriteMealsViewController 而不是导航控制器,它应该可以工作

【讨论】:

    猜你喜欢
    • 2012-01-17
    • 1970-01-01
    • 2014-03-07
    • 2013-03-03
    • 1970-01-01
    • 2012-06-08
    • 2012-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多