如何在UISplitViewController中分配MasterViewController子菜单的委托

时间:2016-02-05 05:04:14

标签: ios swift delegates uisplitviewcontroller

我有一个DetailViewController,它是一个子菜单的委托,在应用程序启动开始时尚未推送。在这里,我想知道将委托属性分配给ViewController的最佳位置在哪里。

这就是我在AppDelegates.swift

所做的
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        dataController = DataController()

        let splitViewController = self.window!.rootViewController as! UISplitViewController
        splitViewController.delegate = self

        let detailNavigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController
        detailNavigationController.topViewController!.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem()
        let mainVC = detailNavigationController.topViewController as! MainViewController
        mainVC.dataController = dataController

        let masterNavigationController = splitViewController.viewControllers[0] as! UINavigationController
        let mainMenuVC = masterNavigationController.topViewController as! MainMenuViewController
        mainMenuVC.dataController = dataController

        self.storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
        let nationsMenuVC = storyboard.instantiateViewControllerWithIdentifier("NationsMenuViewController") as! NationsMenuViewController
        nationsMenuVC.delegate = mainVC

        return true
    }

和子菜单(NationsMenuVC)有:

protocol NationsMenuViewControllerDelegate: class {
    func nationSelected(nation: City?)
}

class NationsMenuViewController: UITableViewController {

    var nations:[City] = []
    var moc: NSManagedObjectContext!
    weak var delegate: NationsMenuViewControllerDelegate?

    override func viewDidLoad() {
.
.
.

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let selectedCity = self.nations[indexPath.row]
    self.delegate?.nationSelected(selectedCity)
    print("clicked at \(selectedCity.name)")
}

}

我确实在我的详细视图控制器(MainVC)中实现了委托协议,但该函数没有触发。我想知道是不是因为委托属性在应用程序启动时分配为弱,并且因为它还没有来自主菜单的任何引用(因为用户还没有实际在主视图控制器级别单击它),委托链接可能已经从记忆中被解雇了?

在这种情况下,哪里是声明尚未加载的viewcontroller的委托的最佳位置?

extension MainViewController: NationsMenuViewControllerDelegate {
    func nationSelected(nation: City?) {
        if let selectedCity = nation {
            let coor:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: selectedCity.latitude!.doubleValue, longitude: selectedCity.longitude!.doubleValue)
            let span:MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: 8.0, longitudeDelta: 8.0)
            let region:MKCoordinateRegion = MKCoordinateRegion(center: coor, span: span)
            self.mapView.setRegion(region, animated: true)
            print("selected \(selectedCity.name)")
        }

    }
}

1 个答案:

答案 0 :(得分:0)

很明显,您在应用程序中创建的nationMenuVC:didFinishLaunchingWithOptions:从未使用过,因此会立即被分配。您可以在NationsMenuViewController的viewDidLoad中设置委托。您可能想知道如何访问NationsMenuViewController的viewDidLoad中的mainVC。你可以在AppDelegate中创建一个函数来解决这个问题

 func  mainVC() -> MainViewController{

    let splitViewController = self.window!.rootViewController as! UISplitViewController


            let detailNavigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController

       let mainVC = detailNavigationController.topViewController as! MainViewController
      return mainVC

    }

现在你可以在NationsMenuViewController的viewDidLoad中调用它并设置委托:

self.delegate  = UIApplication.sharedApplication().delegate. mainVC()

HTH