[英]I am using objective-c classes in swift2 , how i can implement delegate methods and bind that delegate
When you add objective-c classes in swift project, you'll be prompted for 在swift项目中添加Objective-C类时,系统会提示您输入
'Create bridging header' -> click on 'create bridging header'. “创建桥接头” ->单击“创建桥接头”。 Now you can use objective-c code in your swift.
现在,您可以快速使用Objective-C代码。
For more detail refer 有关更多详细信息, 请参阅
Modifying the solution as per your problem: 根据您的问题修改解决方案:
//Objective-c class
// Test.h
#import <Foundation/Foundation.h>
@protocol TestDelegate <NSObject>
- (void)testMethod;
@end
@interface Test : NSObject
@property (weak, nonatomic) id<TestDelegate> delegate;
@end
//Test.m
#import "Test.h"
@implementation Test
@synthesize delegate;
- (void)callDelegate
{
[delegate testMethod];
}
@end
//Swift bridging header
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#import "Test.h"
//Swift class
class ViewController: UIViewController, TestDelegate {
.
.
.
func testMethod() {
print("Delegate");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.