ios - How to call the func in ViewController from appdelegate in Swift? -
i trying call function in viewcontroller 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() // additional setup after loading view, typically nib. } func fileurl(url: nsurl){ externalfile = url println("externalfile = \(externalfile)") } } in appdelegate.swift , call firstviewcontroller.fileurl() , try call first.fileurl().
when call firstviewcontroller.fileurl(url) , show cannot invoke 'fileurl' argument list of type '(nsurl)'.
when call first.fileurl(url) , crash , error log fatal error: unexpectedly found nil while unwrapping optional value.
did missing ? in advance.
since first object nil should first allocate new object using below code :
func application(application: uiapplication, handleopenurl url: nsurl) -> bool { let first = firstviewcontroller() first.fileurl(url) return true }
Comments
Post a Comment