【发布时间】:2017-10-06 06:42:03
【问题描述】:
假设同一项目中有两个类,一个在swift中,另一个在objective-c类中。
在 swift 类中我声明了委托,我想在目标 c 类中设置该委托。
我已经按照以下方式完成了......
import UIKit
@objc public protocol SwiftLoginVCDelegate: class {
}
@objc public class SwiftLoginViewController: UIViewController {
@IBOutlet var txtUserName: UITextField!
@IBOutlet var txtPassword: UITextField!
@IBOutlet var btnLogin: UIButton!
var delegate: SwiftLoginVCDelegate?
override public func viewDidLoad() {
super.viewDidLoad()
self.txtPassword.text = "XYZ"
self.txtPassword.text = "123"
self.btnLogin.backgroundColor = UIColor.blue
}
@objc public func testObjective(){
print("Swift class is integrated in objective c project")
}
目标c类是
#import "Mediator.h"
@class SwiftLoginViewController;
@protocol SwiftLoginVCDelegate;
@interface LoginMediator : Mediator
@end
实现类是
#import "xyz-Swift.h"
@class SwiftLoginViewController;
@implementation LoginMediator
-(void)onRegister
{
// line 1: [(XYZController*)self.viewComponent setDelegate:self];
line 2 : [(SwiftLoginViewController*)self.viewComponent setDelegate:self];
[objectVC testObjective];
}
如果您检查 onRegister 方法,在第 1 行委托使用目标 c 设置,现在已注释,但我想在 swift 4 中设置相同的委托,第 2 行,当我尝试在 swift 4 中设置委托时我收到以下错误
“SwiftLoginViewController”没有可见的@interface 声明 选择器'setdelegate';
注意:
另外一个我可以访问定义的所有 var 和函数 快速类到目标 c 类。我无法设置 在swift Class中声明的objective c Class中的委托。
谁能知道我在上面的代码中做错了什么?感谢所有输入。
【问题讨论】:
-
@objc weak public var delegate: SwiftLoginVCDelegate?
标签: ios objective-c delegates ios11 swift4