Can't get variable in ViewController The 2019 Stack Overflow Developer Survey Results Are InSwift: How to access in AppDelegate variable from the View controller?How to change RootViewController in AppDelegate From Other ViewController?Xcode- how do I bind a combobox value from AppDelegate to ViewController?subscribeNext not getting values in AppDelegateBi-Directional communication between ViewController and AppDelegate with swift 2 for iOSWhy is this variable not printing in this function?How to use variable in other viewController? - SwiftExecuting NSApplicationDelegate Code Before ViewController viewDidLoadCall ViewController when applicationDidEnterBackground in AppDelegateHow to detect any keyboard activity in iOS AppCancelling a view controller from loading if a URL cannot load - Swift 4
How to notate time signature switching consistently every measure
How to support a colleague who finds meetings extremely tiring?
How do I free up internal storage if I don't have any apps downloaded?
"as much details as you can remember"
How to type a long/em dash `—`
What is the motivation for a law requiring 2 parties to consent for recording a conversation
APIPA and LAN Broadcast Domain
Accepted by European university, rejected by all American ones I applied to? Possible reasons?
Mathematics of imaging the black hole
If I can cast sorceries at instant speed, can I use sorcery-speed activated abilities at instant speed?
Are spiders unable to hurt humans, especially very small spiders?
Why can't devices on different VLANs, but on the same subnet, communicate?
Loose spokes after only a few rides
I am an eight letter word. What am I?
Does HR tell a hiring manager about salary negotiations?
Old scifi movie from the 50s or 60s with men in solid red uniforms who interrogate a spy from the past
Falsification in Math vs Science
Star Trek - X-shaped Item on Regula/Orbital Office Starbases
What is the meaning of Triage in Cybersec world?
What is this business jet?
Relationship between Gromov-Witten and Taubes' Gromov invariant
Is it possible for absolutely everyone to attain enlightenment?
A word that means fill it to the required quantity
Deal with toxic manager when you can't quit
Can't get variable in ViewController
The 2019 Stack Overflow Developer Survey Results Are InSwift: How to access in AppDelegate variable from the View controller?How to change RootViewController in AppDelegate From Other ViewController?Xcode- how do I bind a combobox value from AppDelegate to ViewController?subscribeNext not getting values in AppDelegateBi-Directional communication between ViewController and AppDelegate with swift 2 for iOSWhy is this variable not printing in this function?How to use variable in other viewController? - SwiftExecuting NSApplicationDelegate Code Before ViewController viewDidLoadCall ViewController when applicationDidEnterBackground in AppDelegateHow to detect any keyboard activity in iOS AppCancelling a view controller from loading if a URL cannot load - Swift 4
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I want to get the ViewController variable From AppDelegate, I tried :
//1.
print(ViewController().strName)
//2.
let vc = ViewController()
print(vc.strName)
//3.
let vc = UIApplication.shared.keyWindow!.rootViewController as! ViewController
print(vc.strName)
All not working, the print value is empty...
The code in ViewController:
var strName : String = ""
override func viewDidLoad()
strName = "Tom"
xcode swift4 appdelegate
add a comment |
I want to get the ViewController variable From AppDelegate, I tried :
//1.
print(ViewController().strName)
//2.
let vc = ViewController()
print(vc.strName)
//3.
let vc = UIApplication.shared.keyWindow!.rootViewController as! ViewController
print(vc.strName)
All not working, the print value is empty...
The code in ViewController:
var strName : String = ""
override func viewDidLoad()
strName = "Tom"
xcode swift4 appdelegate
1
Can you share youViewController
class?
– Rakesha Shastri
Mar 22 at 5:00
Any answer work for you? If not plz share your ViewController Code for more information.
– jogshardik
Mar 23 at 4:55
Sorry reply too late. The code in ViewController is updated! Thanks. And the "print(...)" code actually is in notification but it doesn't matter right?
– Edem
Mar 25 at 2:44
add a comment |
I want to get the ViewController variable From AppDelegate, I tried :
//1.
print(ViewController().strName)
//2.
let vc = ViewController()
print(vc.strName)
//3.
let vc = UIApplication.shared.keyWindow!.rootViewController as! ViewController
print(vc.strName)
All not working, the print value is empty...
The code in ViewController:
var strName : String = ""
override func viewDidLoad()
strName = "Tom"
xcode swift4 appdelegate
I want to get the ViewController variable From AppDelegate, I tried :
//1.
print(ViewController().strName)
//2.
let vc = ViewController()
print(vc.strName)
//3.
let vc = UIApplication.shared.keyWindow!.rootViewController as! ViewController
print(vc.strName)
All not working, the print value is empty...
The code in ViewController:
var strName : String = ""
override func viewDidLoad()
strName = "Tom"
xcode swift4 appdelegate
xcode swift4 appdelegate
edited Mar 25 at 2:46
Edem
asked Mar 22 at 4:31
EdemEdem
124
124
1
Can you share youViewController
class?
– Rakesha Shastri
Mar 22 at 5:00
Any answer work for you? If not plz share your ViewController Code for more information.
– jogshardik
Mar 23 at 4:55
Sorry reply too late. The code in ViewController is updated! Thanks. And the "print(...)" code actually is in notification but it doesn't matter right?
– Edem
Mar 25 at 2:44
add a comment |
1
Can you share youViewController
class?
– Rakesha Shastri
Mar 22 at 5:00
Any answer work for you? If not plz share your ViewController Code for more information.
– jogshardik
Mar 23 at 4:55
Sorry reply too late. The code in ViewController is updated! Thanks. And the "print(...)" code actually is in notification but it doesn't matter right?
– Edem
Mar 25 at 2:44
1
1
Can you share you
ViewController
class?– Rakesha Shastri
Mar 22 at 5:00
Can you share you
ViewController
class?– Rakesha Shastri
Mar 22 at 5:00
Any answer work for you? If not plz share your ViewController Code for more information.
– jogshardik
Mar 23 at 4:55
Any answer work for you? If not plz share your ViewController Code for more information.
– jogshardik
Mar 23 at 4:55
Sorry reply too late. The code in ViewController is updated! Thanks. And the "print(...)" code actually is in notification but it doesn't matter right?
– Edem
Mar 25 at 2:44
Sorry reply too late. The code in ViewController is updated! Thanks. And the "print(...)" code actually is in notification but it doesn't matter right?
– Edem
Mar 25 at 2:44
add a comment |
2 Answers
2
active
oldest
votes
I suggest to have a check in this link Swift: How to access in AppDelegate variable from the View controller?
else try in view controller,
class ViewController: UIViewController
var strName = "someString" // some value
In AppDelegate,
var mainViewController = ViewController()
func applicationWillEnterForeground(_ application: UIApplication)
_ = self.window?.rootViewController as? ViewController
print(mainViewController.strName)
This has a error message at "let viewController = self.window?.rootViewController as? ViewController"
– Edem
Mar 25 at 2:42
@Edem, just try now the edited (or)section
– AzeTech
Mar 25 at 5:55
@Edem, check the edited answer now, check whether u have navigation controller in view controller.... I have tried its working and I can print the value in appdelegate (or else try this is did finish launch section in app delegate )
– AzeTech
Mar 25 at 6:10
Yes, no error message, but still get empty value....
– Edem
Mar 25 at 6:56
1
Oh Yeh.......... I know, thank you a lot.
– Edem
Mar 25 at 9:17
|
show 4 more comments
First you need to set the storyboard identifier in interface builder
Than you have to your initiate your viewcontroller object using following code.
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let viewcontroller = storyboard.instantiateViewController(withIdentifier: "ViewController") as? ViewController
Here you can change storyboard name according to your storyboard name.
After you got the controller object you can use easily find your viewcontroller variable.
Why bring storyboards into it? You're adding complexity where it's not needed.
– Caleb
Mar 22 at 6:11
This has an error at "class AppDelegate: UIResponder, UIApplicationDelegate,UNUserNotificationCenterDelegate {", it's shows " Thread 1: signal SIGABRT".
– Edem
Mar 25 at 2:41
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%2f55292959%2fcant-get-variable-in-viewcontroller%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I suggest to have a check in this link Swift: How to access in AppDelegate variable from the View controller?
else try in view controller,
class ViewController: UIViewController
var strName = "someString" // some value
In AppDelegate,
var mainViewController = ViewController()
func applicationWillEnterForeground(_ application: UIApplication)
_ = self.window?.rootViewController as? ViewController
print(mainViewController.strName)
This has a error message at "let viewController = self.window?.rootViewController as? ViewController"
– Edem
Mar 25 at 2:42
@Edem, just try now the edited (or)section
– AzeTech
Mar 25 at 5:55
@Edem, check the edited answer now, check whether u have navigation controller in view controller.... I have tried its working and I can print the value in appdelegate (or else try this is did finish launch section in app delegate )
– AzeTech
Mar 25 at 6:10
Yes, no error message, but still get empty value....
– Edem
Mar 25 at 6:56
1
Oh Yeh.......... I know, thank you a lot.
– Edem
Mar 25 at 9:17
|
show 4 more comments
I suggest to have a check in this link Swift: How to access in AppDelegate variable from the View controller?
else try in view controller,
class ViewController: UIViewController
var strName = "someString" // some value
In AppDelegate,
var mainViewController = ViewController()
func applicationWillEnterForeground(_ application: UIApplication)
_ = self.window?.rootViewController as? ViewController
print(mainViewController.strName)
This has a error message at "let viewController = self.window?.rootViewController as? ViewController"
– Edem
Mar 25 at 2:42
@Edem, just try now the edited (or)section
– AzeTech
Mar 25 at 5:55
@Edem, check the edited answer now, check whether u have navigation controller in view controller.... I have tried its working and I can print the value in appdelegate (or else try this is did finish launch section in app delegate )
– AzeTech
Mar 25 at 6:10
Yes, no error message, but still get empty value....
– Edem
Mar 25 at 6:56
1
Oh Yeh.......... I know, thank you a lot.
– Edem
Mar 25 at 9:17
|
show 4 more comments
I suggest to have a check in this link Swift: How to access in AppDelegate variable from the View controller?
else try in view controller,
class ViewController: UIViewController
var strName = "someString" // some value
In AppDelegate,
var mainViewController = ViewController()
func applicationWillEnterForeground(_ application: UIApplication)
_ = self.window?.rootViewController as? ViewController
print(mainViewController.strName)
I suggest to have a check in this link Swift: How to access in AppDelegate variable from the View controller?
else try in view controller,
class ViewController: UIViewController
var strName = "someString" // some value
In AppDelegate,
var mainViewController = ViewController()
func applicationWillEnterForeground(_ application: UIApplication)
_ = self.window?.rootViewController as? ViewController
print(mainViewController.strName)
edited Mar 25 at 6:02
answered Mar 22 at 4:52
AzeTechAzeTech
10929
10929
This has a error message at "let viewController = self.window?.rootViewController as? ViewController"
– Edem
Mar 25 at 2:42
@Edem, just try now the edited (or)section
– AzeTech
Mar 25 at 5:55
@Edem, check the edited answer now, check whether u have navigation controller in view controller.... I have tried its working and I can print the value in appdelegate (or else try this is did finish launch section in app delegate )
– AzeTech
Mar 25 at 6:10
Yes, no error message, but still get empty value....
– Edem
Mar 25 at 6:56
1
Oh Yeh.......... I know, thank you a lot.
– Edem
Mar 25 at 9:17
|
show 4 more comments
This has a error message at "let viewController = self.window?.rootViewController as? ViewController"
– Edem
Mar 25 at 2:42
@Edem, just try now the edited (or)section
– AzeTech
Mar 25 at 5:55
@Edem, check the edited answer now, check whether u have navigation controller in view controller.... I have tried its working and I can print the value in appdelegate (or else try this is did finish launch section in app delegate )
– AzeTech
Mar 25 at 6:10
Yes, no error message, but still get empty value....
– Edem
Mar 25 at 6:56
1
Oh Yeh.......... I know, thank you a lot.
– Edem
Mar 25 at 9:17
This has a error message at "let viewController = self.window?.rootViewController as? ViewController"
– Edem
Mar 25 at 2:42
This has a error message at "let viewController = self.window?.rootViewController as? ViewController"
– Edem
Mar 25 at 2:42
@Edem, just try now the edited (or)section
– AzeTech
Mar 25 at 5:55
@Edem, just try now the edited (or)section
– AzeTech
Mar 25 at 5:55
@Edem, check the edited answer now, check whether u have navigation controller in view controller.... I have tried its working and I can print the value in appdelegate (or else try this is did finish launch section in app delegate )
– AzeTech
Mar 25 at 6:10
@Edem, check the edited answer now, check whether u have navigation controller in view controller.... I have tried its working and I can print the value in appdelegate (or else try this is did finish launch section in app delegate )
– AzeTech
Mar 25 at 6:10
Yes, no error message, but still get empty value....
– Edem
Mar 25 at 6:56
Yes, no error message, but still get empty value....
– Edem
Mar 25 at 6:56
1
1
Oh Yeh.......... I know, thank you a lot.
– Edem
Mar 25 at 9:17
Oh Yeh.......... I know, thank you a lot.
– Edem
Mar 25 at 9:17
|
show 4 more comments
First you need to set the storyboard identifier in interface builder
Than you have to your initiate your viewcontroller object using following code.
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let viewcontroller = storyboard.instantiateViewController(withIdentifier: "ViewController") as? ViewController
Here you can change storyboard name according to your storyboard name.
After you got the controller object you can use easily find your viewcontroller variable.
Why bring storyboards into it? You're adding complexity where it's not needed.
– Caleb
Mar 22 at 6:11
This has an error at "class AppDelegate: UIResponder, UIApplicationDelegate,UNUserNotificationCenterDelegate {", it's shows " Thread 1: signal SIGABRT".
– Edem
Mar 25 at 2:41
add a comment |
First you need to set the storyboard identifier in interface builder
Than you have to your initiate your viewcontroller object using following code.
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let viewcontroller = storyboard.instantiateViewController(withIdentifier: "ViewController") as? ViewController
Here you can change storyboard name according to your storyboard name.
After you got the controller object you can use easily find your viewcontroller variable.
Why bring storyboards into it? You're adding complexity where it's not needed.
– Caleb
Mar 22 at 6:11
This has an error at "class AppDelegate: UIResponder, UIApplicationDelegate,UNUserNotificationCenterDelegate {", it's shows " Thread 1: signal SIGABRT".
– Edem
Mar 25 at 2:41
add a comment |
First you need to set the storyboard identifier in interface builder
Than you have to your initiate your viewcontroller object using following code.
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let viewcontroller = storyboard.instantiateViewController(withIdentifier: "ViewController") as? ViewController
Here you can change storyboard name according to your storyboard name.
After you got the controller object you can use easily find your viewcontroller variable.
First you need to set the storyboard identifier in interface builder
Than you have to your initiate your viewcontroller object using following code.
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let viewcontroller = storyboard.instantiateViewController(withIdentifier: "ViewController") as? ViewController
Here you can change storyboard name according to your storyboard name.
After you got the controller object you can use easily find your viewcontroller variable.
answered Mar 22 at 4:55
jogshardikjogshardik
8531920
8531920
Why bring storyboards into it? You're adding complexity where it's not needed.
– Caleb
Mar 22 at 6:11
This has an error at "class AppDelegate: UIResponder, UIApplicationDelegate,UNUserNotificationCenterDelegate {", it's shows " Thread 1: signal SIGABRT".
– Edem
Mar 25 at 2:41
add a comment |
Why bring storyboards into it? You're adding complexity where it's not needed.
– Caleb
Mar 22 at 6:11
This has an error at "class AppDelegate: UIResponder, UIApplicationDelegate,UNUserNotificationCenterDelegate {", it's shows " Thread 1: signal SIGABRT".
– Edem
Mar 25 at 2:41
Why bring storyboards into it? You're adding complexity where it's not needed.
– Caleb
Mar 22 at 6:11
Why bring storyboards into it? You're adding complexity where it's not needed.
– Caleb
Mar 22 at 6:11
This has an error at "class AppDelegate: UIResponder, UIApplicationDelegate,UNUserNotificationCenterDelegate {", it's shows " Thread 1: signal SIGABRT".
– Edem
Mar 25 at 2:41
This has an error at "class AppDelegate: UIResponder, UIApplicationDelegate,UNUserNotificationCenterDelegate {", it's shows " Thread 1: signal SIGABRT".
– Edem
Mar 25 at 2:41
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%2f55292959%2fcant-get-variable-in-viewcontroller%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
1
Can you share you
ViewController
class?– Rakesha Shastri
Mar 22 at 5:00
Any answer work for you? If not plz share your ViewController Code for more information.
– jogshardik
Mar 23 at 4:55
Sorry reply too late. The code in ViewController is updated! Thanks. And the "print(...)" code actually is in notification but it doesn't matter right?
– Edem
Mar 25 at 2:44