4

Note: It is not a duplicate one to any question. I could not find any question with some action method and creating an instance for a swift based view controller.

I am calling a swift based view controller in objective c class .m file.

In my ViewController.m I have imported bridge header

#import "ViewController.h"
#import "Project-Bridging-Header.h"

I want to call Swift based view controller from the below action

- (IBAction)tappedButnGo:(UIButton *)sender {

     SwiftViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"SwiftViewController"];
}

Here I am getting an error

use of undeclared identifier SwiftViewController

and below is swift class

import UIKit

@objc class SwiftViewController: UIViewController {
 //so on

and my BridgingHeader is empty.

Can anyone help me on loading SwiftViewController when I click on Objective C's button action?

3 Answers 3

8

You need to import following statement in your Controller.h file

#import "ProductModuleName-swift.h"

Here PrductModuleName is your project name

Hope this will help you

Sign up to request clarification or add additional context in comments.

7 Comments

#import "AutoLayout-Swift.h", AutoLayout is my project name.
How can it allow me to import when there is no file with projectname-swift.h. It is throwing same error file not found
where have you write statement?
I am importing it in ViewController.m which is Obj-c class.
@NiravD, my project name is X-Ample so how can i do this...? and also im getting the file not found error when X-Ample-swift.h or XAmple-swift.h
|
5

Check the storyboard SwiftViewController identifier like following image you need to set StoryBoard ID of your SwiftViewController from identiry Inspector :

Following is a steps to use swift class in objective c project:

  • Create New project and select Objective C language.
  • After that when you create new class at that time you get the popup like:

enter image description here

  • Create Bridge header and then go to the build setting and search with Defines Module and make its value YES

  • Now you need to create your swiftVeiwControllers identifire like following code and go to the first viewcontroller

    enter image description here

  • Now you need to import #import "obj_swift-Swift.h". here obj_swift is your project name so its look like #import "yourprojectname-Swift.h"


That's it now you just need to do code like following:

- (IBAction)PushSwift:(id)sender {

     SwiftViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"SwiftViewController"];

    [self.navigationController pushViewController:vc animated:YES];
} 

Here is the sample demo code:

https://github.com/nitingohel/obj_swift

6 Comments

not working, still it is showing the same error. I am unable to create an instance to my swiftclass to use it.
Thanks bro, I think , in #import "yourprojectname-Swift.h" here s(swift) is small.
Its working without small also, but I have a doubt here, with small only the above answer is working, why?
i think that not case sensitive
But If I keep it Swift, throwing file not found error.
|
0

I quite came up with a different situation where used Xib files instead of view controllers:

If some is looking for it, this will be the correct method:

  1. Create a new CocoaTouchClass with xib file: enter image description here

  2. Assign Restoration ID to the Xib file enter image description here

  3. Put bridging header file as others have mentioned earlier in this question. "Project Target Name-Bridging-Header.h"

  4. import that header to the objective c class file.

  5. Use following code to call to the Xib file.

    UIViewController *vc = [[UIViewController alloc] initWithNibName:@"SwiftViewControllerNibRestorationID" bundle:[NSBundle mainBundle]]; [self.navigationController pushViewController:vc animated:YES];

Here ActivityFollowViewController is my SwiftViewControllerNibRestorationID

Comments

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.