gpt4 book ai didi

objective-c - 如何将信息从 Objective-c 的 View Controller 传递到 Swift Storyboard

转载 作者:行者123 更新时间:2023-11-30 10:47:46 24 4
gpt4 key购买 nike

我开始从事一个已经在 ObjectiveC 中启动且没有 Storyboard 的项目。我正在 Swift 和 Storyboard 中制作新部分,但现在当我尝试将信息(UIImage)传递到 Storyboard 的 Controller 时遇到问题。这就是我进入 ViewController 的方式:

-(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];

但它也会崩溃,因为(我认为)它没有实例化 Storyboard,因此无法识别所有 IBOutlet 等等......

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

提前非常感谢

最佳答案

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

但是您的错误表明,[YourApp.CustomNavController setExpenseImage:] 未实现。这意味着您的 View Controller 嵌入在导航 Controller 中。并且您正在尝试通过此代码获取导航 Controller

[addExpenseStoryboard 
instantiateViewControllerWithIdentifier:@"expenseNavController"]

所以你的createExpenseVC实际上是Navigation Controller 类型,而不是CreateExpenseViewController。

这就是为什么它没有获得费用图像。

这就是你的问题所在

这是你可以做的..

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];

关于objective-c - 如何将信息从 Objective-c 的 View Controller 传递到 Swift Storyboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55394711/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com