2016-07-04 7 views
2

모달을 닫은 후에 뷰 컨트롤러에서 함수를 호출하려고합니다. 나는이 일을하기 위해 많은 시간을 보냈으며, 내가 찾은 모든 반응은 효과가 없었다. 나는 다른 사람들의 지시를 따르고 프로토콜을 설정했지만 여전히 작동하지 않습니다.모달을 Swift에서 닫은 후 함수를 실행하십시오.

MainController : 프로토콜

:

class ViewController: UIViewController, UIPopoverPresentationControllerDelegate, loadStoreDelegate{ 

그럼 난 그런 기능 ID가

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

ModalViewController를 사용하려면

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

를 사용하는 모달 트리거
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 (거짓, 완료 : 없음)'. 그래서 그것은 "해산 된 이후"가 아닙니다. – matt

+0

'didSelectRowAtIndexPath'에 중단 점을 설정하여 어떤 일이 발생했는지 확인 했습니까? – Paulw11

+0

예 위임자로부터 nil 값을 얻습니다. –

답변

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) 
      } 
     } 
    } 
관련 문제