user2526811
user2526811

Reputation: 1253

iOS:Swift Access button in AppDelegate

ViewController.swift

@IBOutlet weak var btn_stop: UIButton!

AppDelegate.swift

let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("ViewController") as! ViewController

viewController.btn_stop.hidden=false

Error - gives me Fatal error & app Crash

Upvotes: 2

Views: 600

Answers (1)

Lodewijck
Lodewijck

Reputation: 416

Access your ViewController via the rootViewController option:

let viewController = self.window!.rootViewController as! ViewController
viewController.btn_stop.hidden = false // Found nil error

But even in this case it will find nil. The viewController is instantiated but not loaded yet. You should probably unhide the button in the viewDidLoad function of the ViewController.

Upvotes: 1

Related Questions