21

I would like to write in the text or csv (prefer) file the variable from the view controller.

Indeed I am doing a drawing app and I would like to write in the file the current position of the finger.

class ViewController: UIViewController {var lastPoint = CGPoint.zeroPoint ... }

I would like to have access to lastPoint.x and lastPoint.y from the AppDelegate. how I could do that ? Thank you.

Yann Amouyal
  • 211
  • 1
  • 2
  • 3

4 Answers4

42

Your question is full of confusion but if that's what you are looking for:

You can access the appDelegate by getting a reference to it like that:

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

after that if you have store a property called lastPoint in your appDelegate you can access its components very simply like that:

let x = appDelegate.lastPoint.x
let y = appDelegate.lastPoint.y

If you need to access your viewController properties from the AppDelegate, then I suggest having a reference to your view controller in your appdelegate:

var myViewController: ViewController!

then when your view controller is created you can store a reference to it in the appdelegate property:

If your create your view controller outside of your appDelegate:

Swift 1-2 syntax

var theViewController = ViewController()
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.myViewController = theViewController

Swift 3-4 syntax

var theViewController = ViewController()
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.myViewController = theViewController

If you create your view controller inside of your appDelegate:

self.myViewController = ViewController()

After that you can access your data from your viewcontroller from your appdelegate just by accessing its property like that:

let x = self.myViewController.lastPoint.x
let y = self.myViewController.lastPoint.y
The Tom
  • 2,790
  • 6
  • 29
  • 33
  • Actually it s the opposite that I need. From appDelegate I need to have access to variable lastPoint.x and lastPoint.y (found in the ViewController) – Yann Amouyal Jun 23 '15 at 10:08
  • 1
    it s doesn't work. There is a mistake at this line: appDelegate.myViewController = theViewController "expected declaration" And the also : let x = self.myViewController.lastPoint.x let y = self.myViewController.lastPoint.y – Yann Amouyal Jun 23 '15 at 10:53
  • Could you tell us where you put those lines ? – The Tom Jun 23 '15 at 11:43
  • inside: class AppDelegate: UIResponder, UIApplicationDelegate {...} (instead of 3 dots) because I want to use X and Y in the application function. – Yann Amouyal Jun 24 '15 at 00:58
  • All of this needs to be declared in the body of the function. You can put this in applicationDidFinishLaunching for example or in a custom function that you can call when your ViewController has been created – The Tom Jun 24 '15 at 05:05
3

Swift 3 Update

    var theViewController = ViewController()
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.myViewController = theViewController
Sébastien REMY
  • 2,399
  • 21
  • 39
2

You can create a BaseViewController and write this

class BaseViewController {
  lazy var appDelegate : AppDelegate {
     return UIApplication.shared.delegate as? AppDelegate
  }
}

and inherit other viewcontrollers with BaseViewController and access this by

class ViewController : BaseViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    print(self.appDelegate?.lastPoint.x)
    print(self.appDelegate?.lastPoint.x)
}}
Agent Smith
  • 2,873
  • 17
  • 32
1

I did manage to access my ViewController from the App delegate by creating an instance of ViewController in AppDelegate.swift, like:

var mainViewController = ViewController()

func applicationWillEnterForeground(_ application: UIApplication) {
    mainViewController.myVariable = "Hello".
}

Still, I don't understand how does the AppDelegate "know" that mainViewController is supposed to point to that particular ViewController. As my application has a single view and a single ViewController, that's fine, but what it I had multiple ViewControllers associated with different UIViews? Appreciate if anyone here can shed a light into that. Best Regards, Andre

Andre Guerra
  • 1,117
  • 1
  • 9
  • 18
  • Just found out that AppDelegate is not referencing my actual ViewController.swift. It seems to have created a new ViewController instance inside my AppDelegate, so I still don't know how to access my existing VC instance. Appreciate any clarification on this topic. – Andre Guerra Sep 27 '17 at 14:09
  • Just found this response to what I need here in SO: https://stackoverflow.com/questions/43831571/swift-ios-how-to-run-function-in-viewcontroller-from-appdelegate – Andre Guerra Sep 27 '17 at 15:01