【问题标题】:Why am I getting error to implement delegate to pass value from a child swift class to a parent objective C class?为什么我在实现委托以将值从子 swift 类传递给父目标 C 类时出错?
【发布时间】:2019-11-12 12:50:26
【问题描述】:

我试图将值从 swift 类传递到目标 C 类,但出现错误。错误是

“由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[ MainViewController childViewControllerResponseWithAsset:]: 无法识别的选择器发送到实例 0x7f969a133c00"

ChildViewController 快速类:

@objc protocol ChildViewControllerDelegate
{
func childViewControllerResponse(asset:AVAsset)
}

class ChildViewController:UIViewController
{
@objc var delegate: ChildViewControllerDelegate?
@objc var asset:AVAsset!

@objc func apply() {
self.delegate?.childViewControllerResponse(asset: self.Video())

//dismiss view
self.navigationController?.popViewController(animated: false)
}

}

MainViewController 目标 C 类:

#import "Project-Swift.h"
@interface MainViewController()<ChildViewControllerDelegate>
{

-(IBAction)ButtonPressed:(UIButton *)sender{

 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
 ChildViewController *vc = (ChildViewController*)[storyboard instantiateViewControllerWithIdentifier:@"ChildViewController"];
 AVAsset *asset = self.originalVideoAsset;
 vc.asset = asset;
 vc.delegate = self;
 [self.navigationController pushViewController:vc animated:YES];

 }

 // Define Delegate Method
 -(void)childViewControllerResponse:(AVAsset*)asset
 {
 self.originalVideoAsset = asset;
 }

}

我将如何解决这个问题或者我做错了什么?

【问题讨论】:

  • 我建议你依靠 Xcode 的自动完成功能,帮助你找到像这样的导入 API 的精确拼写。

标签: objective-c swift delegates


【解决方案1】:

Swift 方法childViewControllerResponse 变成了Objective-C 方法childViewControllerResponseWithAsset。这就是 Swift 到 ObjC 转换的工作原理。因此,您应该将您的 Objective-C 方法重命名为:

 -(void)childViewControllerResponseWithAsset:(AVAsset*)asset
 {
    self.originalVideoAsset = asset;
 }

或者,您可以将@objc 属性应用于 Swift 方法,并指定您想要的名称:

@objc(childViewControllerResponse)
func childViewControllerResponse(asset:AVAsset)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2014-11-09
    相关资源
    最近更新 更多