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

How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?