我有一个 dylib 框架,其中包含一些快速创建的 UIView 子类,我已经完成这些子类以使用新的 @IBDesignable
和 @IBInspectable
东西。
假设我有一个名为 MyTextField.swift
的 UITextField
子类,如下所示:
MyTextField.swift
@IBDesignable class MyTextField: UITextField {
// some properties etc.
// content is irrelevant
}
现在它在 InterfaceBuilder 中编译并运行良好。但是我需要做的是将这个特殊的子类导入到我的 Controller 的 Objective C 实现中,以便在运行时在代码中设置一个属性。
框架(名为 myViews
)有一个名为 myViews.h
的 header ,我可以像这样将其导入到 Controller header 中:
MyViewController.h:
@import myViews;
#import <UIKit/UIKit.h>
#import "myViews.h"
@interface MyViewController : UIViewController
@property(weak, nonatomic) IBOutlet MyTextField *txtName; // <-- This is the problem!
@end
这就是我被困的地方!因此类 MyTextField
在导入的 header 中是未知的。 myViews.h
是自动生成的。我试图在其中导入桥接 header 但没有成功。
myViews.h:
#import <UIKit/UIKit.h>
//! Project version number for myViews.
FOUNDATION_EXPORT double myViewsVersionNumber;
//! Project version string for myViews.
FOUNDATION_EXPORT const unsigned char myViewsVersionString[];
// In this header, you should import all the public headers of your framework
// using statements like #import <myViews/PublicHeader.h>
#import <myViews/MyTextField-Swift.h> // <-- This is what I've tried to import here which doesn't work as well.
希望有人能帮助我。提前致谢。
最佳答案
在您的 objective-c 类中,尝试像这样导入您的 swift 模块:
#import "<ModuleName>-Swift.h"
其中 ModuleName 是包含 swift 代码的模块的名称
关于ios - Swift Framework <-> ObjC 互操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25769404/