委托不适用于SplitView

时间:2016-08-23 12:31:48

标签: ios swift uisplitviewcontroller

我有一个带有主应用(左)和详细(右)视图的SplitView,适用于iPad应用。我想要的是,当我单击主视图中的一个按钮时,在父拆分视图上调用委托方法并推送一个新视图,但是从不调用委托方法。为什么呢?

这里是代码:

主视图:

protocol newAlbumDelegate{
   func openAlbum()
}
class newAlbumController: UIViewController {
   var delegate:newAlbumDelegate?

   @IBAction func buttonClick(sender:UIButton){
      delegate?.openAlbum()
   }
}

SPLITVIEW

import UIKit

class AlbumSplitController: UISplitViewController, newAlbunDelegate {

   var newAlbumToAdd:newAlbumController = newAlbumController()

   //Delegate
   func openAlbum(){
      print("Do something")
   }

   override func viewDidLoad(){
      super.viewDidLoad()
      self.newAlbumToAdd.delegate = self
   }

任何帮助?

此致

1 个答案:

答案 0 :(得分:0)

masterViewController内,将splitViewController指定为代理人:

protocol newAlbumDelegate{
    func openAlbum()
}
class newAlbumController: UIViewController {
    var delegate:newAlbumDelegate?

    @IBAction func buttonClick(sender:UIButton){
        delegate?.openAlbum()
    }

    override func viewDidLoad(){
        super.viewDidLoad()
        let splitView = self.splitViewController as! AlbumSplitController
        self.delegate = splitView
    }
}

您的splitViewController变为:

class AlbumSplitController: UISplitViewController, newAlbumDelegate {

   //Delegate
   func openAlbum(){
      print("Do something")
   }
}