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?
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.
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.
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.
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.
Apparently it's quite easy.
@objc
so that it's available to your Objective-C ones.For example:
import Foundation
// Belongs to target 'TestProject'
@objc class SwiftController: UIViewController {
//... truncated
}
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With