假设swift中有两个类,另一个是同一个项目中的objective-c类。如何使用swift 4在目标c类中符合swift类委托?
在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中声明的目标c类中的 Delegate。
任何人都可以知道我在做什么错在上面的代码?所有的投入都赞赏。
我已经这样做了,但仍然得到相同的错误“没有可见@interface为'SwiftLoginViewController'声明选择器'setdelegate'; ”我能够访问快速类变量和方法,但我无法设置在Swift类中,在Objective C类中声明委托。 –
一切都已经交给你了!我甚至在这个答案中提供了一个示例项目。您需要学习如何在Objective-C项目中使用Swift类。祝你好运。 – Glenn