简体   繁体   English

我在swift2中使用Objective-C类,我如何实现委托方法并绑定该委托

[英]I am using objective-c classes in swift2 , how i can implement delegate methods and bind that delegate

在此处输入图片说明 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. 我在用obj-c编写的项目中使用自定义UIView类,现在我想在swift类中绑定委托,如何在swift类中实现委托。

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代码。

  • In your Objective-C bridging header file, import every Objective-C header you want to expose to Swift. 在您的Objective-C桥接头文件中,导入要公开给Swift的每个Objective-C头。
  • 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. 在“构建设置”的“ Swift编译器-代码生成”中,确保下面的“ Objective-C桥接头”构建设置具有桥接头文件的路径。 The path should be relative to your project, similar to the way your Info.plist path is specified in Build Settings. 该路径应相对于您的项目,类似于在“构建设置”中指定Info.plist路径的方式。 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");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在另一个Objective-C类中调用UITextField委托方法? - How can I call UITextField delegate methods in a different Objective-C class? 如何合并Objective-c和Swift委托方法 - how to merge Objective-c and Swift delegate methods 我不明白为什么 Objective-C 委托函数有效而 Swift 委托函数崩溃。 你能给我解释一下吗? - I don't understand why an Objective-C delegate function works and a Swift delegate function crash. Can you explain to me? 我可以将委托作为参数objective-c传递 - Can I pass delegate as a parameter objective-c 如何为cpp类创建一个objective-c委托? - how do I make an objective-c delegate for a cpp class? 我如何在目标c中使用segue制定委托协议 - how i can make delegate protocol using segue in objective c 如何通过Swift扩展实现委托方法 - How do I implement delegate methods through Swift extension Objective-C 中的委托方法和 Swift 中的视图控制器。 如何调用协议? - iOS - Delegate methods in Objective-C and viewcontroller in Swift. How to call protocols? - iOS 如何在 Objective-C 的子类中拦截和转发委托消息? - How can I intercept and forward delegate messages in a subclass in Objective-C? 如何在Objective-C中实现多级委托 - How to implement multi-level delegate in Objective-C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM