如何在objective c中调用swift视图控制器?

时间:2021-07-22 07:37:00

I have a objective c controller, i want to call another controller which is a swift controller from that objc controller?

我有一个objective c控制器,我想调用另一个控制器它是那个objc控制器的swift控制器?

2 个解决方案

#1


9  

Apparently it's quite easy.

显然很容易。

  1. You have to first make sure you're prepending your class declaration with @objc so that it's available to your Objective-C ones.
  2. 您必须首先确保您使用@objc预挂类声明,以便您的Objective-C可以使用它。

For example:

例如:

import Foundation

// Belongs to target 'TestProject'
@objc class SwiftController: UIViewController {

    //... truncated

}
  1. Now you simply have to import the name of your target (that the swift controller belongs to), appended with '-Swift.h'—exposing its interface—like so:

    现在,您只需导入目标的名称(swift控制器所属的名称),并添加“-Swift”。h的暴露其类似于接口:

    #import "TestProject-Swift.h"

    #进口“TestProject-Swift.h”

#2


0  

Create a new file "YourAppName-Bridging-Header.h" and in this file you have to import "Your objective-C view controller.h"

创建一个新的文件“YourAppName-Bridging-Header”。在这个文件中,你必须导入"你的objective-C视图控制器。h"

Go to Build setting and search for bridging header and link with your birdging-header path.

转到构建设置和搜索桥接头和链接您的birdgheader路径。

Now you are able to write code in swift as usual.

现在您可以像往常一样用swift编写代码了。

#1


9  

Apparently it's quite easy.

显然很容易。

  1. You have to first make sure you're prepending your class declaration with @objc so that it's available to your Objective-C ones.
  2. 您必须首先确保您使用@objc预挂类声明,以便您的Objective-C可以使用它。

For example:

例如:

import Foundation

// Belongs to target 'TestProject'
@objc class SwiftController: UIViewController {

    //... truncated

}
  1. Now you simply have to import the name of your target (that the swift controller belongs to), appended with '-Swift.h'—exposing its interface—like so:

    现在,您只需导入目标的名称(swift控制器所属的名称),并添加“-Swift”。h的暴露其类似于接口:

    #import "TestProject-Swift.h"

    #进口“TestProject-Swift.h”

#2


0  

Create a new file "YourAppName-Bridging-Header.h" and in this file you have to import "Your objective-C view controller.h"

创建一个新的文件“YourAppName-Bridging-Header”。在这个文件中,你必须导入"你的objective-C视图控制器。h"

Go to Build setting and search for bridging header and link with your birdging-header path.

转到构建设置和搜索桥接头和链接您的birdgheader路径。

Now you are able to write code in swift as usual.

现在您可以像往常一样用swift编写代码了。