0

enter image description herei 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 1

2

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");
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

I have done that all but i am not able to bind delegate of that customView in swift
What error are you getting while binding your customView? Add that error to your question.
Does not conform to protocol
Have you implemented protocol in your swift2 class? Add little code as well.
No how i can implement i am not able to bind delegate
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.