Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference or call a view controller written in Swift from another controller written in Objective-C? [closed]

I have an Objective-C project, and need hints on how to call a ViewController which is in Swift from another controller in Objective-C?

like image 733
rimtej Avatar asked Aug 25 '15 10:08

rimtej


People also ask

How do I present a view controller from another view controller?

Start a segue from any object that implements an action method, such as a control or gesture recognizer. You may also start segues from table rows and collection view cells. Right-click the control or object in your current view controller. Drag the cursor to the view controller you want to present.

Can I call a method on another view controller?

Instead, if you really need to call a method on another view controller, and there are very valid reasons to do so, though it is more common to set a property than I agree with Andrew, you should refactor the code so that you don’t have to do that.

How to call action from controller context?

var ctrl= new MyController (); ctrl.ControllerContext = ControllerContext; //call action return ctrl.Action (); 1. Create a plain class ( not a controller!) for this and pass arguments- if you have common methods that does not involves html code 2. Create partial methods / html helpers and call from the view - if you have html codes Try this.

Is it possible to instantiate a view controller from storyboard?

Also, stay away from instantiating view controller from the storyboard or Nibs are suggested below, that is not a good idea and a waste of memory. If you have a segue to the other view controller, use that to get to the properties and methods inside it. That’s easy using notifications center to do that.


Video Answer


1 Answers

Apparently it's quite easy.

  1. You have to first make sure you're prepending your class declaration with @objc so that it's available to your Objective-C ones.

For example:

import Foundation

// Belongs to target 'TestProject'
@objc class SwiftController: UIViewController {

    //... truncated

}
  1. Now you simply have to import the name of your target (that the swift controller belongs to), appended with '-Swift.h'—exposing its interface—like so:

    #import "TestProject-Swift.h"

like image 71
dangnabit Avatar answered Oct 12 '22 19:10

dangnabit