Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing data from Objective-C Viewcontroller to Swift ViewController

I'm trying to pass data from an objective-c ViewController to a Swift ViewController and I'm getting this error in my Objective-C controller:

Property 'type' not found on object of type 'UIViewController'

My Objective-C code:

UIViewController *mainController = [self.storyboard instantiateViewControllerWithIdentifier:@"IdentitySelfieViewController"];
mainController.type = "passport";
[self.navigationController pushViewController:mainController animated:YES];

Import into the Bridging-header file:

#import "PassportViewController.h"

And created the corresponding String into my Swift View Controller

let type = ""

Thank you for any help

like image 997
Isabelle Avatar asked Jun 07 '17 08:06

Isabelle


2 Answers

The Solution to your problem is like that :

First in your Swift Controller add the @Objc before the class keyword like that :

@objc class ViewController: UIViewController

and that in that controller create a variable like that :

public var myValue:String  = ""

After this in your Objective-C ViewController you need to import this and keep in mind this is very important.

#import "TargetName-Swift.h"

TargetName should be replaced by your target name. After this you can easily call the variables of that Swift Controller like that:

ViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"VC"];
vc.myValue = @"Hello";
like image 194
Shabir jan Avatar answered Sep 26 '22 18:09

Shabir jan


This is your swift class

Step 1:

class SwiftClassController: UIViewController {
//Required variables
@objc var deviceId: String?
@objc var deviceObj: CustomType?

Step 2:

In Objective C class import #import "YourProjectName-Swift.h"

Step 3:

In your Objective C class access variable from Swift file

YourViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"YourVC"];
vc.deviceId = @"1234567";
vc. deviceObj = yourCustomObject;
like image 26
Naresh Avatar answered Sep 23 '22 18:09

Naresh