6

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

5
  • You need to create an object out of the Objective-C imported class. Commented Jun 7, 2017 at 8:51
  • Oh... may I kindly ask if you have any example to share with me? Thank you Commented Jun 7, 2017 at 8:52
  • I haven't done it for a year, but I think it's somethink like the following. let passportInstance: PassportViewController = PassportViewController() Commented Jun 7, 2017 at 8:53
  • I will investigate this, thank you :) Commented Jun 7, 2017 at 8:54
  • Create Object of Swift class. e.g IdentitySelfieViewController * mainController. Commented Jun 7, 2017 at 9:01

2 Answers 2

7

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";
Sign up to request clarification or add additional context in comments.

7 Comments

You are my hero :) Thank you so much for your time and for your answer.
So my variable is recognized; it seems I can pass it from Objective-C to Swift. How would you retrieve the value in your Swift Controller? Rather than putting this: public var myValue:String = "" - Thanks again
its very simple after passing the value you can just the variable in the Swift Controller , it will have that values that you passed.
or anyone who may do same dumb mistake as me. In step 2, make sure you use (ProjectName)-Swift.h, NOT (ClassName)-Swift.h from stackoverflow.com/questions/24078043/…
In my case Swift4.2, I need to add @objc before the variable declaration. Example: @objc public var myValue:String = ""
|
7

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.