如何在swift中使用两个UIViewControllers,在objective-c中使用其他一个? [重复]

时间:2021-07-24 13:35:27

This question already has an answer here:

这个问题在这里已有答案:

I am rewriting my app which was in Objective-C to Swift. I was wondering if I can use some old UIViewControllers from my previous app in my new application without having to rewrite these in Swift. Is it possible?

我正在重写我在Objective-C到Swift的应用程序。我想知道我是否可以在我的新应用程序中使用我之前的应用程序中的一些旧的UIViewControllers而无需在Swift中重写它们。可能吗?

2 个解决方案

#1


1  

Yes you can use a bridging header.

是的,你可以使用桥接头。

When you import Objective-C files into a swift project (let's call the project MyApp), it should ask if you want to create the bridging header MyApp-Bridging-Header.h. If it doesn't ask, you can always create it yourself, but if you do it this way make sure you include it under the Swift Compiler - Code Generator -> Objective-C Bridging Header in the Build Settings of your project. Inside this file you can write the import for your Objective-C file e.g.:

当您将Objective-C文件导入swift项目(让我们调用项目MyApp)时,它应该询问您是否要创建桥接头MyApp-Bridging-Header.h。如果它没有询问,您可以自己创建它,但如果您这样做,请确保将其包含在项目的构建设置中的Swift编译器 - 代码生成器 - > Objective-C桥接头下。在此文件中,您可以为Objective-C文件编写导入,例如:

#import "MyUIViewController.h"

This will import them to the project, so that they are compatible with the other Swift files. Then in your swift class you can refer to that Objective-C view controller like you would any other swift class, e.g.:

这会将它们导入到项目中,以便它们与其他Swift文件兼容。然后在你的swift类中,你可以像任何其他swift类一样引用Objective-C视图控制器,例如:

let myUIViewController = MyUIViewController()

#2


0  

Yes it will work

I also work with my previous project and i imported my old Objective-C code into the Swift project making Bridge header for accessing that Objective-C UIViewController from Swift class. You directly assign your Objective-C UiViewcontroller class in the StoryBoard UIviewController and no need to rewrite any coad again for UIViewController.if you want to access Objective-C class inside your Swift Controller Class you need to make first Bridge header file then do this.

我还使用我以前的项目,并将我的旧Objective-C代码导入到Swift项目中,使用Bridge头来访问Swift类中的Objective-C UIViewController。你可以直接在StoryBoard UIviewController中分配你的Objective-C UiViewcontroller类,而不需要为UIViewController再次重写任何coad。如果你想在你的Swift控制器类中访问Objective-C类,你需要制作第一个Bridge头文件,然后执行此操作。

如何在swift中使用两个UIViewControllers,在objective-c中使用其他一个? [重复]

 //In Swift Controller you can call your Objective-C Class 
 let myView = MyController() // This is Objective-C controller
 myView.updatemyProfile() // Can call function like Objective-C here

#1


1  

Yes you can use a bridging header.

是的,你可以使用桥接头。

When you import Objective-C files into a swift project (let's call the project MyApp), it should ask if you want to create the bridging header MyApp-Bridging-Header.h. If it doesn't ask, you can always create it yourself, but if you do it this way make sure you include it under the Swift Compiler - Code Generator -> Objective-C Bridging Header in the Build Settings of your project. Inside this file you can write the import for your Objective-C file e.g.:

当您将Objective-C文件导入swift项目(让我们调用项目MyApp)时,它应该询问您是否要创建桥接头MyApp-Bridging-Header.h。如果它没有询问,您可以自己创建它,但如果您这样做,请确保将其包含在项目的构建设置中的Swift编译器 - 代码生成器 - > Objective-C桥接头下。在此文件中,您可以为Objective-C文件编写导入,例如:

#import "MyUIViewController.h"

This will import them to the project, so that they are compatible with the other Swift files. Then in your swift class you can refer to that Objective-C view controller like you would any other swift class, e.g.:

这会将它们导入到项目中,以便它们与其他Swift文件兼容。然后在你的swift类中,你可以像任何其他swift类一样引用Objective-C视图控制器,例如:

let myUIViewController = MyUIViewController()

#2


0  

Yes it will work

I also work with my previous project and i imported my old Objective-C code into the Swift project making Bridge header for accessing that Objective-C UIViewController from Swift class. You directly assign your Objective-C UiViewcontroller class in the StoryBoard UIviewController and no need to rewrite any coad again for UIViewController.if you want to access Objective-C class inside your Swift Controller Class you need to make first Bridge header file then do this.

我还使用我以前的项目,并将我的旧Objective-C代码导入到Swift项目中,使用Bridge头来访问Swift类中的Objective-C UIViewController。你可以直接在StoryBoard UIviewController中分配你的Objective-C UiViewcontroller类,而不需要为UIViewController再次重写任何coad。如果你想在你的Swift控制器类中访问Objective-C类,你需要制作第一个Bridge头文件,然后执行此操作。

如何在swift中使用两个UIViewControllers,在objective-c中使用其他一个? [重复]

 //In Swift Controller you can call your Objective-C Class 
 let myView = MyController() // This is Objective-C controller
 myView.updatemyProfile() // Can call function like Objective-C here