如何将信息从Objective-c的视图控制器传递到Swift故事板

时间:2019-03-28 09:58:23

标签: objective-c swift xcode

我开始在一个已经在ObjectiveC中启动且没有Storyboard的项目中工作。我正在Swift和Storyboard中创建新的部分,但是现在当我尝试将信息(UIImage)传递到Storyboard的控制器时遇到了问题。 这就是我转到ViewControllers的方式:

-(void)expenseCamera:(VOCCustomCameraVC *)camera didFinishPickImage:(UIImage *)
                       image imageCreated:(NSString *)imageCreated 
                       fileName:(NSString *)fileName {

    [camera dismissViewControllerAnimated: YES completion: nil];

         UIStoryboard *addExpenseStoryboard = [UIStoryboard 
                                             storyboardWithName:@"addExpense" bundle:nil];
         CreateExpenseVC *createExpenseVC = [addExpenseStoryboard 
         instantiateViewControllerWithIdentifier:@"expenseNavController"];
         UIImage *compressedImage = [UIImage compressImage: image];
         createExpenseVC.expenseImage = compressedImage; // It crashes here





    //createExpenseVC.delegate = self;
    [self presentViewController: createExpenseVC animated: YES completion: nil];
    CFRunLoopWakeUp(CFRunLoopGetCurrent());
}

如果我取出createExpenseVC.expenseImage = compressedImage,一切都会顺利进行,但是当我尝试传递图像时,会出现此错误:

SIGABRT *** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: '-[YourApp.CustomNavController setExpenseImage:]: unrecognized selector
 sent to instance 0x12e09b200'

目标viewController以这种方式启动

@objcMembers class CreateExpenseVC: UIViewController {


   /* ---- IBOutlets ---- */
    @IBOutlet weak var containerView: UIView!
    @IBOutlet weak var backButton: UIBarButtonItem!
    @objc var expenseImage: UIImage?


然后在containerView中显示另一个viewController,我在其中假装显示图像。

我试图更改用于导航到此的代码

CreateExpenseVC * createExpenseVC = [[CreateExpenseVC alloc] init];

但是它也会崩溃,因为(我认为)它没有实例化情节提要,因此无法识别所有的IBOutlets等等……

那我怎么传递这样的信息?

非常感谢

2 个答案:

答案 0 :(得分:0)

我试图用实现的方式来复制您的问题,对我来说很好。

但是您的错误是,未实现[YourApp.CustomNavController setExpenseImage:]。这意味着您的视图控制器嵌入在导航控制器内部。 而且您正在尝试通过此代码获取导航控制器

[addExpenseStoryboard 
     instantiateViewControllerWithIdentifier:@"expenseNavController"]

因此您的createExpenseVC实际上是导航控制器类型,而不是CreateExpenseViewController类型。

这就是为什么它没有获得expenseImage的原因。

那是你的问题所在

这是您可以做的..

UIStoryboard *addExpenseStoryboard = [UIStoryboard storyboardWithName:@"CreateExpense" bundle:nil];
UINavigationController *navController = [addExpenseStoryboard instantiateViewControllerWithIdentifier:@"ExpenseNavC"];
CreateExpenseViewController *createExpenseVC = navController.viewControllers[0];
UIImage *image = [UIImage imageNamed:@"383975"];

createExpenseVC.expenseImage = image;
[self presentViewController: navController animated: YES completion: nil];

答案 1 :(得分:0)

在处理此问题时,我会检查多项内容。

  • 是否已分配情节提要ID?
  • 是否分配了正确的UIViewController类?
  • UIViewController是否具有视图出口?

检查以下屏幕截图以获取直观指南:

View controller identify inspector screenshot

View controller view outlet screenshot