4

I am in my AppDelegate and I need to refresh the webview placed in my ViewController.

So I've googled how to call a method/variable from appDelegate and I found this:

MyViewController* mainController = (MyViewController*) self.window.rootViewController 

And it looks like it works, but I'm working in Swift and I'm kinda of a noob trying to translate this without any luck.. this is my result but I can't manage to make it work..

ViewController vc = (ViewController) self.window?.rootViewController 

What am I doing wrong? How can I access to something in my viewcontroller from my appDelegate?

Thank you very much and have a nice day

3
  • Why does your app delegate need to do this? It doesn't sound like the app delegate's job. Commented Jan 2, 2015 at 9:18
  • a) You are declaring your vc variable incorrectly - that isn't how you declare a variable with a type in Swift. b) you are casting self.window?.rootViewController to ViewController which is incorrect, and also the wrong type. To be honest, you really need to spend more time reading the Swift introductory references rather that asking for direct code translations on SO. These are very basic concepts. Commented Jan 2, 2015 at 9:20
  • Use NSNotifications for this kind of things. Commented Jan 2, 2015 at 9:34

1 Answer 1

6

Try

let viewController = self.window.rootViewController as UIViewController 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.