2016-07-04 16 views
2

Ich möchte eine Funktion auf einem View-Controller aufrufen, nachdem ein Modal wurde entlassen. Ich habe stundenlang versucht, dies zur Arbeit zu bringen und alle Antworten, die ich gefunden habe, haben nicht funktioniert. Ich habe Anweisungen von anderen gefolgt und ein Protokoll erstellt, aber das funktioniert immer noch nicht.Run-Funktion nach Modal in Swift entlassen

MainController:

class ViewController: UIViewController, UIPopoverPresentationControllerDelegate, loadStoreDelegate{ 

dann die modale auszulösen I

verwenden
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) 
      } 
     } 
    } 

Dann ist die Funktion ID

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

ModalViewController verwenden mag: Das Protokoll

protocol loadStoreDelegate{ 
    func loadStore() 
} 

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

var delegate: loadStoreDelegate? 

Dann rufen Sie die Funktion auf Tableview klicken

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

Sie rufen 'loadStore () '_before_ calling' self.dismissViewControllerAnimated (false, completion: nil) '. Das ist also nicht "nachdem es entlassen wurde". – matt

+0

Haben Sie in Ihrem 'didSelectRowAtIndexPath' einen Haltepunkt gesetzt, um zu sehen, was passiert? – Paulw11

+0

Ja, ich bekomme einen Nullwert vom Delegaten –

Antwort

1

Ihre SelectStoreViewController Klasse hat eine delegate Instanz Eigenschaft. Aber Sie setzen diese Eigenschaft nie auf etwas. So ist es nil wenn Sie self.delegate?.loadStore(). So passiert natürlich nichts.

Ich glaube, Sie so etwas wie dies wollen:

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

In allen Beispielen, die ich gesehen habe, verweisen sie so auf den Delegierten? Worauf setze ich es? –

+0

Zur ViewController-Instanz. Das ist es, was Sie als Delegat haben wollen (das 'loadStoreDelegate'). Deine Frage ist nicht sehr klar, aber ich habe Code hinzugefügt, der zeigt, was ich denke, dass du versuchst zu tun. – matt

+0

Wenn ich dies versuche, hat der selectStoreController keine Delegate-Methode, also kann ich sie nicht einstellen. Die Ctrl-Variable hat eine Delegate-Methode, aber das Problem wird nicht behoben –

0

Nach viel es zu lesen scheint, dass die popover Verwendung ist, was meine gewünschten Ergebnisse aus den Delegierten verhindert. Am Ende habe ich das gelöst, indem ich eine Klasse für die Workstation erstellt habe, die einen Verweis auf die aktuelle Ansicht enthält, und dann eine Funktion, die die benötigte Aktion ausführt.

Klasse:

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) 
     } 
    } 

In meinem Haupt-Controller ich einfach im Popup bin Laden und die aktuelle Ansicht einstellen

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) 
      } 
     } 
    }