Calling to a variable of swift view controller file in Objective-C
I have a project that is a cross of Swift and Objective-C using a bridging-header.
In my main ViewController.swift file, outside of the class declaration, I have this code:
var mainView = ViewController()
In other views that I segue to, I can use this to call a function to run back on the main ViewController by using mainView.runFunction()
How can I call this function in an Objective-C .m implementation file?
Thanks!
ios objective-c swift uiviewcontroller
add a comment |
I have a project that is a cross of Swift and Objective-C using a bridging-header.
In my main ViewController.swift file, outside of the class declaration, I have this code:
var mainView = ViewController()
In other views that I segue to, I can use this to call a function to run back on the main ViewController by using mainView.runFunction()
How can I call this function in an Objective-C .m implementation file?
Thanks!
ios objective-c swift uiviewcontroller
add a comment |
I have a project that is a cross of Swift and Objective-C using a bridging-header.
In my main ViewController.swift file, outside of the class declaration, I have this code:
var mainView = ViewController()
In other views that I segue to, I can use this to call a function to run back on the main ViewController by using mainView.runFunction()
How can I call this function in an Objective-C .m implementation file?
Thanks!
ios objective-c swift uiviewcontroller
I have a project that is a cross of Swift and Objective-C using a bridging-header.
In my main ViewController.swift file, outside of the class declaration, I have this code:
var mainView = ViewController()
In other views that I segue to, I can use this to call a function to run back on the main ViewController by using mainView.runFunction()
How can I call this function in an Objective-C .m implementation file?
Thanks!
ios objective-c swift uiviewcontroller
ios objective-c swift uiviewcontroller
asked Dec 31 '18 at 13:07
RanLearnsRanLearns
2,25433554
2,25433554
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
First of all for using swift in objective-c you need to import TargetName-Swift.h
. Note that it's the target name.
For more information look at this.
You can achieve what you want in this way:
ViewController *mainView = [[UIViewController alloc] init];
[mainView runFunction];
Also you should declare your runFunction
with @objc
to use it in objective-c like below:
@objc func runFunction {
// what you want to do ...
}
Thanks for answering. Even though I have a ViewController.swift file and added@objc
to its class definition, your code pops up the error "Unknown receiver ViewController" - if I add the#import "project-Swift.h"
then the view controller is recognized in the first line of your code, but the second line shows error"No visible @interface for 'UIViewController' declares the selector 'runFunction'"
– RanLearns
Jan 1 at 4:30
@RanLearns answer updated. that was a slip of the tongue!
– Arash Etemad
Jan 1 at 8:22
add a comment |
Follow this apple article and done : Load Swift in Objective-C.
Or I already did is a "trick" using "@objc" key, look at this little explanation: What is @objc attribute, one easy way is just create a helper function that will be visible to your Objective-c class and done like:
@objc func retrieveMainView() -> UIViewController { return MyViewController() }
And you call this from your objective-c class, maybe you need to anotate your swift class with @objc, look at this two reference and you will get the idea and figure out for sure .
add a comment |
In your Objective file i.e. .m file add below import statement:
import "<ProjectName>-Swift.h"
For example your project name is MyProject, so import statement would look like:
import "MyProject-Swift.h"
And call your function like: [mainView runFunction];
I hope this will help. You can also refer one of my answer:
How can I import Swift code to Objective-C?
Thanks for answering. I believe my import statement is correct (no errors upon running) but the [mainView runFunction]; is erroring asUse of undeclared identifier 'mainView'
– RanLearns
Jan 1 at 4:38
Is your issue resolved? @RanLearns
– Dheeraj D
Jan 3 at 5:48
Yes, thank you. I marked another answer as correct. I had to instantiate a ViewController before I could call a function on it.
– RanLearns
Jan 4 at 13:20
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53987875%2fcalling-to-a-variable-of-swift-view-controller-file-in-objective-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
First of all for using swift in objective-c you need to import TargetName-Swift.h
. Note that it's the target name.
For more information look at this.
You can achieve what you want in this way:
ViewController *mainView = [[UIViewController alloc] init];
[mainView runFunction];
Also you should declare your runFunction
with @objc
to use it in objective-c like below:
@objc func runFunction {
// what you want to do ...
}
Thanks for answering. Even though I have a ViewController.swift file and added@objc
to its class definition, your code pops up the error "Unknown receiver ViewController" - if I add the#import "project-Swift.h"
then the view controller is recognized in the first line of your code, but the second line shows error"No visible @interface for 'UIViewController' declares the selector 'runFunction'"
– RanLearns
Jan 1 at 4:30
@RanLearns answer updated. that was a slip of the tongue!
– Arash Etemad
Jan 1 at 8:22
add a comment |
First of all for using swift in objective-c you need to import TargetName-Swift.h
. Note that it's the target name.
For more information look at this.
You can achieve what you want in this way:
ViewController *mainView = [[UIViewController alloc] init];
[mainView runFunction];
Also you should declare your runFunction
with @objc
to use it in objective-c like below:
@objc func runFunction {
// what you want to do ...
}
Thanks for answering. Even though I have a ViewController.swift file and added@objc
to its class definition, your code pops up the error "Unknown receiver ViewController" - if I add the#import "project-Swift.h"
then the view controller is recognized in the first line of your code, but the second line shows error"No visible @interface for 'UIViewController' declares the selector 'runFunction'"
– RanLearns
Jan 1 at 4:30
@RanLearns answer updated. that was a slip of the tongue!
– Arash Etemad
Jan 1 at 8:22
add a comment |
First of all for using swift in objective-c you need to import TargetName-Swift.h
. Note that it's the target name.
For more information look at this.
You can achieve what you want in this way:
ViewController *mainView = [[UIViewController alloc] init];
[mainView runFunction];
Also you should declare your runFunction
with @objc
to use it in objective-c like below:
@objc func runFunction {
// what you want to do ...
}
First of all for using swift in objective-c you need to import TargetName-Swift.h
. Note that it's the target name.
For more information look at this.
You can achieve what you want in this way:
ViewController *mainView = [[UIViewController alloc] init];
[mainView runFunction];
Also you should declare your runFunction
with @objc
to use it in objective-c like below:
@objc func runFunction {
// what you want to do ...
}
edited Jan 5 at 7:35
answered Dec 31 '18 at 14:02
Arash EtemadArash Etemad
685418
685418
Thanks for answering. Even though I have a ViewController.swift file and added@objc
to its class definition, your code pops up the error "Unknown receiver ViewController" - if I add the#import "project-Swift.h"
then the view controller is recognized in the first line of your code, but the second line shows error"No visible @interface for 'UIViewController' declares the selector 'runFunction'"
– RanLearns
Jan 1 at 4:30
@RanLearns answer updated. that was a slip of the tongue!
– Arash Etemad
Jan 1 at 8:22
add a comment |
Thanks for answering. Even though I have a ViewController.swift file and added@objc
to its class definition, your code pops up the error "Unknown receiver ViewController" - if I add the#import "project-Swift.h"
then the view controller is recognized in the first line of your code, but the second line shows error"No visible @interface for 'UIViewController' declares the selector 'runFunction'"
– RanLearns
Jan 1 at 4:30
@RanLearns answer updated. that was a slip of the tongue!
– Arash Etemad
Jan 1 at 8:22
Thanks for answering. Even though I have a ViewController.swift file and added
@objc
to its class definition, your code pops up the error "Unknown receiver ViewController" - if I add the #import "project-Swift.h"
then the view controller is recognized in the first line of your code, but the second line shows error "No visible @interface for 'UIViewController' declares the selector 'runFunction'"
– RanLearns
Jan 1 at 4:30
Thanks for answering. Even though I have a ViewController.swift file and added
@objc
to its class definition, your code pops up the error "Unknown receiver ViewController" - if I add the #import "project-Swift.h"
then the view controller is recognized in the first line of your code, but the second line shows error "No visible @interface for 'UIViewController' declares the selector 'runFunction'"
– RanLearns
Jan 1 at 4:30
@RanLearns answer updated. that was a slip of the tongue!
– Arash Etemad
Jan 1 at 8:22
@RanLearns answer updated. that was a slip of the tongue!
– Arash Etemad
Jan 1 at 8:22
add a comment |
Follow this apple article and done : Load Swift in Objective-C.
Or I already did is a "trick" using "@objc" key, look at this little explanation: What is @objc attribute, one easy way is just create a helper function that will be visible to your Objective-c class and done like:
@objc func retrieveMainView() -> UIViewController { return MyViewController() }
And you call this from your objective-c class, maybe you need to anotate your swift class with @objc, look at this two reference and you will get the idea and figure out for sure .
add a comment |
Follow this apple article and done : Load Swift in Objective-C.
Or I already did is a "trick" using "@objc" key, look at this little explanation: What is @objc attribute, one easy way is just create a helper function that will be visible to your Objective-c class and done like:
@objc func retrieveMainView() -> UIViewController { return MyViewController() }
And you call this from your objective-c class, maybe you need to anotate your swift class with @objc, look at this two reference and you will get the idea and figure out for sure .
add a comment |
Follow this apple article and done : Load Swift in Objective-C.
Or I already did is a "trick" using "@objc" key, look at this little explanation: What is @objc attribute, one easy way is just create a helper function that will be visible to your Objective-c class and done like:
@objc func retrieveMainView() -> UIViewController { return MyViewController() }
And you call this from your objective-c class, maybe you need to anotate your swift class with @objc, look at this two reference and you will get the idea and figure out for sure .
Follow this apple article and done : Load Swift in Objective-C.
Or I already did is a "trick" using "@objc" key, look at this little explanation: What is @objc attribute, one easy way is just create a helper function that will be visible to your Objective-c class and done like:
@objc func retrieveMainView() -> UIViewController { return MyViewController() }
And you call this from your objective-c class, maybe you need to anotate your swift class with @objc, look at this two reference and you will get the idea and figure out for sure .
answered Dec 31 '18 at 13:40
Felipe FlorencioFelipe Florencio
1848
1848
add a comment |
add a comment |
In your Objective file i.e. .m file add below import statement:
import "<ProjectName>-Swift.h"
For example your project name is MyProject, so import statement would look like:
import "MyProject-Swift.h"
And call your function like: [mainView runFunction];
I hope this will help. You can also refer one of my answer:
How can I import Swift code to Objective-C?
Thanks for answering. I believe my import statement is correct (no errors upon running) but the [mainView runFunction]; is erroring asUse of undeclared identifier 'mainView'
– RanLearns
Jan 1 at 4:38
Is your issue resolved? @RanLearns
– Dheeraj D
Jan 3 at 5:48
Yes, thank you. I marked another answer as correct. I had to instantiate a ViewController before I could call a function on it.
– RanLearns
Jan 4 at 13:20
add a comment |
In your Objective file i.e. .m file add below import statement:
import "<ProjectName>-Swift.h"
For example your project name is MyProject, so import statement would look like:
import "MyProject-Swift.h"
And call your function like: [mainView runFunction];
I hope this will help. You can also refer one of my answer:
How can I import Swift code to Objective-C?
Thanks for answering. I believe my import statement is correct (no errors upon running) but the [mainView runFunction]; is erroring asUse of undeclared identifier 'mainView'
– RanLearns
Jan 1 at 4:38
Is your issue resolved? @RanLearns
– Dheeraj D
Jan 3 at 5:48
Yes, thank you. I marked another answer as correct. I had to instantiate a ViewController before I could call a function on it.
– RanLearns
Jan 4 at 13:20
add a comment |
In your Objective file i.e. .m file add below import statement:
import "<ProjectName>-Swift.h"
For example your project name is MyProject, so import statement would look like:
import "MyProject-Swift.h"
And call your function like: [mainView runFunction];
I hope this will help. You can also refer one of my answer:
How can I import Swift code to Objective-C?
In your Objective file i.e. .m file add below import statement:
import "<ProjectName>-Swift.h"
For example your project name is MyProject, so import statement would look like:
import "MyProject-Swift.h"
And call your function like: [mainView runFunction];
I hope this will help. You can also refer one of my answer:
How can I import Swift code to Objective-C?
answered Dec 31 '18 at 14:04
Dheeraj DDheeraj D
2,47221226
2,47221226
Thanks for answering. I believe my import statement is correct (no errors upon running) but the [mainView runFunction]; is erroring asUse of undeclared identifier 'mainView'
– RanLearns
Jan 1 at 4:38
Is your issue resolved? @RanLearns
– Dheeraj D
Jan 3 at 5:48
Yes, thank you. I marked another answer as correct. I had to instantiate a ViewController before I could call a function on it.
– RanLearns
Jan 4 at 13:20
add a comment |
Thanks for answering. I believe my import statement is correct (no errors upon running) but the [mainView runFunction]; is erroring asUse of undeclared identifier 'mainView'
– RanLearns
Jan 1 at 4:38
Is your issue resolved? @RanLearns
– Dheeraj D
Jan 3 at 5:48
Yes, thank you. I marked another answer as correct. I had to instantiate a ViewController before I could call a function on it.
– RanLearns
Jan 4 at 13:20
Thanks for answering. I believe my import statement is correct (no errors upon running) but the [mainView runFunction]; is erroring as
Use of undeclared identifier 'mainView'
– RanLearns
Jan 1 at 4:38
Thanks for answering. I believe my import statement is correct (no errors upon running) but the [mainView runFunction]; is erroring as
Use of undeclared identifier 'mainView'
– RanLearns
Jan 1 at 4:38
Is your issue resolved? @RanLearns
– Dheeraj D
Jan 3 at 5:48
Is your issue resolved? @RanLearns
– Dheeraj D
Jan 3 at 5:48
Yes, thank you. I marked another answer as correct. I had to instantiate a ViewController before I could call a function on it.
– RanLearns
Jan 4 at 13:20
Yes, thank you. I marked another answer as correct. I had to instantiate a ViewController before I could call a function on it.
– RanLearns
Jan 4 at 13:20
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53987875%2fcalling-to-a-variable-of-swift-view-controller-file-in-objective-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown