user5907771
user5907771

Reputation:

How to navigate from ObjC file to swift file in objC project?

My project name is ObjCSwift. After creating the project, I create a new swift file SecondViewController.swift with xib.

I have imported #import "ObjCSwift-Swift.h" in AppDelegate.m and this is my code for ViewController.m

#import "ViewController.h"
#import "ObjCSwift-Swift.h"

@interface ViewController ()
@property (nonatomic, strong) SecondViewController* controller;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)goToSwiftAction:(id)sender {
    _controller = [[SecondViewController alloc] init];
//    _controller = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
_controller.view.backgroundColor = [UIColor purpleColor];
[self.navigationController pushViewController:_controller animated:true];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end

The project runs successfully with no warning. But when I tap the button goToSwiftAction, the method is called but the view is not navigating.

I also set the Embedded Code Contains Swift Code to YES.

What am I missing? Please help.

Upvotes: 1

Views: 1542

Answers (2)

abdul sathar
abdul sathar

Reputation: 2396

This one helps to me

Navigate Objective C to Swift Controller

on your objective C Class use below for navigation to swift controller

YourSwiftViewController *Obj = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"swiftIdentifier"];

[self presentViewController:Obj animated:YES completion:nil];

This image helps how to set identifier in storyboard

enter image description here

Similar Navigate Swift to Objective C Controller

let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let obj : YourViewController = storyboard.instantiateViewControllerWithIdentifier("YourViewControllerIdentifier") as! YourViewController
self.presentViewController(vc, animated: true, completion: nil)

Upvotes: 1

Anbu.Karthik
Anbu.Karthik

Reputation: 82756

try this

From storyboard to xib

SecondViewController *viewController=[[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil]; // ensure once your nib name is correctly or not.

[self presentViewController:viewController animated:YES completion:nil];

if it is In case of underwith in NavigatinController

[self.navigationController pushViewController:viewController animated:YES];

Upvotes: 1

Related Questions