i am using custom UIView class in my project which is written in obj-c and now i want to bind delegate in swift class and how can i implement that in swift class.
1 Answer
When you add objective-c classes in swift project, you'll be prompted for
'Create bridging header' -> click on 'create bridging header'. Now you can use objective-c code in your swift.
- In your Objective-C bridging header file, import every Objective-C header you want to expose to Swift.
- In Build Settings, in Swift Compiler - Code Generation, make sure the Objective-C Bridging Header build setting under has a path to the bridging header file. The path should be relative to your project, similar to the way your Info.plist path is specified in Build Settings. In most cases, you should not need to modify this setting.
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");
}
}
7 Comments
Janak LN
I have done that all but i am not able to bind delegate of that customView in swift
Anni S
What error are you getting while binding your customView? Add that error to your question.
Janak LN
Does not conform to protocol
Anni S
Have you implemented protocol in your swift2 class? Add little code as well.
Janak LN
No how i can implement i am not able to bind delegate
|