2016-07-04 55 views
2

我想在模態被解除後調用視圖控制器上的功能。我花了幾個小時試圖讓這個工作,我發現所有的反應都沒有奏效。我遵循其他人的指示並建立了一個協議,但這仍然不起作用。Swift中模態被解除後的運行功能

MainController:

class ViewController: UIViewController, UIPopoverPresentationControllerDelegate, loadStoreDelegate{ 

然後以觸發予使用

func displaySelectStorePopup(){ 
     if let viewController = self.storyboard?.instantiateViewControllerWithIdentifier("SelectStoreView"){ 
      let selectStoreController = viewController 
      selectStoreController.modalPresentationStyle = .Popover 
      if let sctrl = selectStoreController.popoverPresentationController{ 
       sctrl.delegate = self 
       sctrl.sourceView = self.view 
       sctrl.sourceRect = CGRectMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds),0,0) 

       sctrl.permittedArrowDirections = UIPopoverArrowDirection() 
       delay(0.1){ 
        sctrl.passthroughViews = nil 
       } 

       selectStoreController.modalInPopover = true 

       selectStoreController.preferredContentSize = CGSizeMake(400, 400) 

       self.presentViewController(selectStoreController, animated: true, completion: nil) 
      } 
     } 
    } 

然後函數ID喜歡用

func loadStore() { 
     print(2) 
     //let vc : AnyObject! = self.storyboard!.instantiateViewControllerWithIdentifier("DashboardView") 
     //self.showViewController(vc as! UIViewController, sender: vc) 
    } 

ModalViewController模態: 協議

protocol loadStoreDelegate{ 
    func loadStore() 
} 

class SelectStoreViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{... 

var delegate: loadStoreDelegate? 

然後調用上的tableview點擊

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){ 
     self.delegate?.loadStore() 
     if(tableView == selectStoreTable){ 
      currentStore = userStores[indexPath.row] 
      self.dismissViewControllerAnimated(false, completion: nil) 
     } 
    } 
+0

要調用'loadStore( )'_before_調用'self.dismissViewControllerAnimated(false,completion:nil)'。所以這不是「被解僱後」。 – matt

+0

您是否在'didSelectRowAtIndexPath'中設置了一個斷點來查看發生了什麼? – Paulw11

+0

是的,我從代表中得到了一個零值 –

回答

1

你SelectStoreViewController類有一個delegate實例屬性的功能。但是你從來沒有把這個屬性設置爲任何東西。所以當你self.delegate?.loadStore()時,它是nil。所以自然沒有任何反應

我想你想是這樣的:

func displaySelectStorePopup(){ 
    if let viewController = self.storyboard?.instantiateViewControllerWithIdentifier("SelectStoreView") as? SelectStoreViewController { 
     let selectStoreController = viewController 
     selectStoreController.delegate = self // * 
     // ... and so on ... 
+0

在我看到的所有例子中,他們是如何引用委託的?我該如何設置它? –

+0

給ViewController實例。那就是你想要作爲委託人('loadStoreDelegate')的人。你的問題不是很清楚,但我已經添加了一些代碼,顯示了我認爲你正在嘗試做的事情。 – matt

+0

當我嘗試這個selectStoreController沒有委託方法,所以我不能設置它。 ctrl變量確實有一個委託方法,但這並不能解決問題 –

0

很多看完之後似乎使用酥料餅是什麼阻止從委託我想要的結果。我最終通過爲工作站創建一個類來解決這個問題,該工作站擁有對當前視圖的引用,然後是一個運行我需要的操作的函數。

類:

class Workstation{ 
    var currentView: UIViewController? = nil 
    var currentStore : Store? = nil 

    func loadInStore(store: Store){ 
     currentStore = store 
     if let view = currentView{ 
      let vc : AnyObject! = view.storyboard!.instantiateViewControllerWithIdentifier("DashboardView") 
      view.showViewController(vc as! UIViewController, sender: vc) 
     } 
    } 

    class var workstationInstance: Workstation{ 
     struct Static { 
      static let instance = Workstation() 
     } 
     return Static.instance 
    } 
} 

SecondController:

override func viewDidDisappear(animated: Bool) { 
     if let store = selectedStore{ 
      Workstation.workstationInstance.loadInStore(store) 
     } 
    } 

在我的主控制器我只是加載在彈出式窗口,設置當前視圖

override func viewDidAppear(animated: Bool) { 
    Workstation.workstationInstance.currentView = self 
} 

func displaySelectStorePopup(){ 
     if let viewController = self.storyboard?.instantiateViewControllerWithIdentifier("SelectStoreView"){ 
      let selectStoreController = viewController 
      selectStoreController.modalPresentationStyle = .Popover 
      if let sctrl = selectStoreController.popoverPresentationController{ 
       sctrl.sourceView = self.view 
       sctrl.sourceRect = CGRectMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds),0,0) 

       sctrl.permittedArrowDirections = UIPopoverArrowDirection() 
       delay(0.1){ 
        sctrl.passthroughViews = nil 
       } 

       selectStoreController.modalInPopover = true 

       selectStoreController.preferredContentSize = CGSizeMake(400, 400) 

       self.presentViewController(selectStoreController, animated: true, completion: nil) 
      } 
     } 
    }