2

Im looking for a solution to navigate to a viewcontroller in objective-C from a Swift viewcontroller.

Im using storybord with a navigation controller.

My Swift 2.0 code are as follows:

if ((user) != nil) {
                    let alert = UIAlertView(title: "Success", message: "You are now Logged In", delegate: self, cancelButtonTitle: "OK")
                    alert.show()

                    dispatch_async(dispatch_get_main_queue(), { () -> Void in
                        let viewController:UIViewController = UIStoryboard(name: "MainStoryboard", bundle: nil).instantiateViewControllerWithIdentifier("Menu")
                        self.presentViewController(viewController, animated: true, completion: nil)
                    })

                } else {
                    let alert = UIAlertView(title: "Error", message: "\(error)", delegate: self, cancelButtonTitle: "OK")
                    alert.show()
                }
            })
        }

The Viewcontroller in Objective-C i want to present are named : CategoriesController

Smile
  • 667
  • 1
  • 5
  • 21
Flemming Ovesen
  • 305
  • 2
  • 4
  • 15
  • what exactly is your question / problem? There is no problem whatsoever transitioning to a viewcontroller written in the other language. – luk2302 Jan 02 '16 at 10:19
  • When im adding this code : dispatch_async(dispatch_get_main_queue(), { () -> Void in let viewController:UIViewController = UIStoryboard(name: "MainStoryboard", bundle: nil).instantiateViewControllerWithIdentifier("Menu") self.presentViewController(CategoriesController, animated: true, completion: nil) in the Swift file to present the "CategoriesController" im getting the error message : "Use of unresolved identifier 'CategoriesController'" – Flemming Ovesen Jan 02 '16 at 10:33
  • Im trying to navigate from at viewcontroller in Swift from Storyboard to a viewcontroller in Objective-C that are not in my Storyboard ? – Flemming Ovesen Jan 02 '16 at 10:37

2 Answers2

0

to navigate from swiftcontroller to objective-c controller the code is same but only you have to import objective c file in bridging-header . for adding bridge-header you can see tutorial over here Create bridge file

see code i have used in my project is same . i just import "ObjectiveViewController.h" in bridge header file

 let mycards = ObjectiveViewController(nibName:"ObjectiveViewController", bundle : nil)
 self.navigationController?.pushViewController(mycards, animated: true) and its working 
Jaydeep Patel
  • 1,699
  • 17
  • 30
  • The code you gave me works with no errors. But now after "#import CategoriesController.h" in the Bridging-Header.h, this causes fault in other Objective-C files. #import #import #import #import #import #import #import #import #import "CategoriesController.h" – Flemming Ovesen Jan 02 '16 at 10:50
  • Fault message "Cannot find interface decleration for 'ControlBase', superclass of 'FilterBaseController' – Flemming Ovesen Jan 02 '16 at 10:52
  • have you import all `#import #import #import #import #import #import #import #import #import "CategoriesController.h"` in headre file? – Jaydeep Patel Jan 02 '16 at 11:07
  • Yes. It all works fine until i added the "#import "CategoriesController.h" file in the Bridging-header.h file. After that i get the fault message. – Flemming Ovesen Jan 02 '16 at 11:13
  • Add this to your header: `#include ` – Jaydeep Patel Jan 02 '16 at 11:27
  • I have the "#import " in the 'CategoriesController.h'. Is it there you mean ? – Flemming Ovesen Jan 02 '16 at 11:58
  • check this http://stackoverflow.com/questions/8994911/cannot-find-interface-declaration-for-nsobject (second answer) – Jaydeep Patel Jan 02 '16 at 12:01
  • Solved the issue. The Bridging-Header was not added to my Build Setting. – Flemming Ovesen Jan 02 '16 at 13:03
  • How can this be programmed in Swift 2.0 : CategoriesController *mainViewController = [[CategoriesController alloc] init]; [mainViewController setConfigDictionary:_configDictionary]; [self.navigationController initWithRootViewController:mainViewController]; – Flemming Ovesen Jan 02 '16 at 13:03
  • if self is not have init with navigation controller `let mycards = CategoriesController(nibName:"CategoriesController", bundle : nil) self.presentViewController(UINavigationController(rootViewController: mycards), animated: true, completion: nil)` and if self is init view navigation controller then `let mycards = CategoriesController(nibName:"CategoriesController", bundle : nil) self.navigationController?.pushViewController(mycards, animated: true)` – Jaydeep Patel Jan 02 '16 at 13:56
  • 1
    I solved the issue. I made a new viewcontroller in Storyboard and imbedded in a navigation controller. Then i named the new viewcontroller 'CategoriesController'. Then im using this code to show the main viewcontroller : let viewController:UIViewController = UIStoryboard(name: "MainStoryboard", bundle: nil).instantiateViewControllerWithIdentifier("CategoriesController") self.showViewController(viewController, sender: true) – Flemming Ovesen Jan 03 '16 at 08:24
0

So suppose I have a viewcontroller MemberShipVC.h and .m written in objective C

I have a another viewcontroller CertifiedVC.swift written in swift

so here I want to navigate in MemberShipVC from CertifiedVC.swift

Step 1- import "MemberShipVC.h" in YourApp-Bridging-Header.h file

Step 2- on any action in CertifiedVC.swift put below line

let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let mVC : MemberShipVC = storyBoard.instantiateViewController(withIdentifier: "MemberShipVC") as! MemberShipVC
self.navigationController?.pushViewController(mVC, animated: true)

Thats all you need to do. Hope this helps.

Nagendra Tripathi
  • 923
  • 12
  • 23