ios - Calling custom delegate from AppDelegate in Swift -
i have delegate in appdelegate.swift fires once app opens app url scheme.
appdelegate.swift
func application(application: uiapplication, openurl url: nsurl, sourceapplication: string?, annotation: anyobject) -> bool { return true }
it fires fine when app opens app url scheme, when function fires, want notify viewcontroller. thought custom made delegate, , let appdelegate notify ever implements delegate has opened app.
mydelegate.swift
protocol mydelegate { func opened(hasbeenopened: bool!) }
then viewcontroller implements delegate
loginviewcontroller.swift
import uikit /* class has more code , other functions, illustration of problem, removed other unrelated things.*/ class loginviewcontroller: uiviewcontroller, mydelegate { func opened(hasbeenopened: bool!) { print(hasbeenopened) } }
so far good, let's return openurl() function in appdelegate.swift , try call mydelegate.opened(). lost.
appdelegate.swift
func application(application: uiapplication, openurl url: nsurl, sourceapplication: string?, annotation: anyobject) -> bool { print("openurl delegate running") if var delegate = mydelegate?() { delegate.opened(true) } return true }
the console prints "openurl delegate running", it's running, delegate variable becomes nil
. there initialization i'm missing?
i can't seem figure out how call own custom delegate appdelegate. there way notify viewcontrollers has happened? or bad idea overall, there way that's considered better?
thank in advance, appreciate help.
you trying initialise protocol (var delegate = mydelegate?()
) not possible.
the way use delegates registering conformance on class, doing in loginviewcontroller
, , calling method defined in protocol directly on instance of class.
for example:
var loginviewcontroller = // instance of loginviewcontroller loginviewcontroller.opened(true)
in case don't have access instance, nor considered practice keep reference view controller in app delegate. think paradigm looking notifications. have @ documentation nsnotificationcenter or nshipster article on notifications.
Comments
Post a Comment