1

I am trying to call the function in ViewController from appdelegate in Swift?

In Appdelegate.swift

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var first: FirstViewController!

    func application(application: UIApplication, handleOpenURL url: NSURL) -> Bool {

        FirstViewController.fileurl(url)
        first.fileurl(url)
        return true
    }
}

In FirstViewController.swift

import UIKit
class FirstViewController: UIViewController {
    var externalFile: NSURL!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    func fileurl(url: NSURL){
        externalFile = url
        println("externalFile = \(externalFile)")
}
}

In Appdelegate.swift , I call the FirstViewController.fileurl() and also try to call first.fileurl().

When I call FirstViewController.fileurl(url) , it show Cannot invoke 'fileurl' with an argument list of type '(NSURL)'.

When I call first.fileurl(url) , it crash and the error log is fatal error: unexpectedly found nil while unwrapping an Optional value.

Did I missing something ? Thanks in advance.

Martin
  • 2,813
  • 11
  • 43
  • 66

3 Answers3

2

It's your UIViewController called first that is not initialized. If this done in another place e.g. in storyboard during loading of your app, you only need to assign the rootviewcontroller to your variable. One way of doing this is:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var first: COBezierDemoViewController?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        if let firstViewController = self.window?.rootViewController as? COBezierDemoViewController {
            self.first = firstViewController
        }

        return true
    }

    func application(application: UIApplication, handleOpenURL url: NSURL) -> Bool {
        first?.fileurl(url)
    }
}
knutigro
  • 1,082
  • 2
  • 10
  • 20
1

Since your first object is nil you should first allocate a new object by using the below code :

func application(application: UIApplication, handleOpenURL url: NSURL) -> Bool {

        let first = FirstViewController()
        first.fileurl(url)
        return true
    }
Vizllx
  • 9,135
  • 1
  • 41
  • 79
Vinay Jain
  • 1,653
  • 20
  • 28
0

You need to use your storyboard. Give identifier to your ViewController using storyboard.

let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = mainStoryboard.instantiateViewControllerWithIdentifier("<Controller ID>") as FirstViewController
vc.fileurl(url)
phnmnn
  • 12,813
  • 11
  • 47
  • 64
  • Hi , I have try. But it show 'AppDelegate.Type' does not have a member named 'mainStoryboard' – Martin Jul 23 '15 at 11:31
  • see this answer: http://stackoverflow.com/questions/8186375/storyboard-refer-to-viewcontroller-in-appdelegate – phnmnn Jul 23 '15 at 11:35