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;








0















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"











share|improve this question



















  • 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


















0















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"











share|improve this question



















  • 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














0












0








0


0






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"











share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 25 at 2:46







Edem

















asked Mar 22 at 4:31









EdemEdem

124




124







  • 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













  • 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








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













2 Answers
2






active

oldest

votes


















0














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)






share|improve this answer

























  • 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


















0














First you need to set the storyboard identifier in interface builder



instanciate



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.






share|improve this answer























  • 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












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
);



);













draft saved

draft discarded


















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









0














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)






share|improve this answer

























  • 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















0














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)






share|improve this answer

























  • 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













0












0








0







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)






share|improve this answer















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)







share|improve this answer














share|improve this answer



share|improve this answer








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

















  • 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













0














First you need to set the storyboard identifier in interface builder



instanciate



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.






share|improve this answer























  • 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
















0














First you need to set the storyboard identifier in interface builder



instanciate



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.






share|improve this answer























  • 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














0












0








0







First you need to set the storyboard identifier in interface builder



instanciate



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.






share|improve this answer













First you need to set the storyboard identifier in interface builder



instanciate



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.







share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript