Search code examples
iosswiftibeacon

Ibeacon Navigation Controller and ViewController


Hi I am a beginner in stackoverflow and swift. I have a question about Ibeacon in swift. I follow this Tutorial and work.

Unfortunately, I want to add a Navigation Controller, and there is an issue I can't solve it.

In storyboard, I embed in Navigation Controller and set Storyboard Entry Point.

    let viewController:ViewController = window!.rootViewController as! ViewController
    viewController.beacons = beacons as! [CLBeacon]
    viewController.tableView!.reloadData()

show:

Could not cast value of type 'UINavigationController' (0x3439285c) to 'Ibeacon.ViewController' (0xda2e8).

Ok, I Google for it,and I try :

    let storyboard :UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let viewController : ViewController = storyboard.instantiateViewControllerWithIdentifier("Ibeacon_menu") as! ViewController 
    viewController.beacons = beacons as! [CLBeacon]
    viewController.tableView!.reloadData()

It still not working......

Ok, I open debug mode to see its value.

I found beacons and tableView are nil. But I don't know how to fix it.

Please Help me!! Thanks.


Solution

  • If you are doing in appdelegate didfinishLoading, then try it

    let navigationController = window!.rootViewController as! UINavigationController
    let controller = navigationController.viewControllers[0] as! ViewController
    viewController.beacons = beacons as! [CLBeacon]
    viewController.tableView!.reloadData()