-2

From my appdelegate I need to execute a method that is from a viewcontroller.

Please, can anybody tell me how to easily have the possibility to call whatever method I need from my appdelegate?

A lot of questions regarding this argument but none of these are useful/complete/right, so please avoid to post URL to other topics: I already checked all questions but I really can't find any precise answer regarding doing this in Swift. :)

2
  • You need a reference to the view controller in your app delegate. Then just call the method? theViewController.<method>....! Mar 26, 2015 at 14:43
  • 2
    You only need to get a pointer to the controller whose method you want to call. Conceptually, this is a simple problem, but there's no way to give you a "precise" answer without knowing specifically what controller you're trying to access, and how it fits into your controller hierarchy.
    – rdelmar
    Mar 26, 2015 at 15:13

4 Answers 4

7

It depends on how you've arranged your view controllers but here's an example from a simple iPhone master/detail project.

let root : UINavigationController = self.window!.rootViewController! as UINavigationController
let master : MasterViewController = root.topViewController as MasterViewController
println("\(master.description)")
0
0

The way I did it was to search for the view controller I want, starting on AppDelegate's var window: UIWindow? and then going deeper until I find it. I originally tried to implement NSNotification but this is not recommended on swift (I think computed property is a good replace for that, but it don't work in this case).

This is how I did for my tab based application with a NavigationController on top of my ViewController:

if let rootViewController = self.window?.rootViewController as? UITabBarController
    if let viewControllers = rootViewController.viewControllers {
        for navigationController in viewControllers {
            if let yourViewController = navigationController.topViewController as? YourCustomViewController {
                if yourViewController.hasSomeFlag {
                    yourViewController.theMethod()
                }
                break
            }
        }
    }
}
-1

You can do it with notifications, just add observer to your viewcontroller, post a notification from your app delegate, this observer will catch it and run a function you specify.

this tutorial should help get you started: https://www.codefellows.org/blog/how-to-implement-an-nsnotification-observer-in-swift

-2

Try this

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
    // Override point for customization after application launch.

    return true
}

func customMethod()
{

}
}

Call custom method from ViewController

class ViewController: UIViewController {

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

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    appDelegate.customMethod()
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.