Search code examples
iosobjective-cswiftaction

Load a Swift based ViewController from Objective C class's button action


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?


Solution

  • 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