在dismissViewController回调中刷新presentViewController的UI时意外发现nil

时间:2016-09-02 19:40:17

标签: ios swift model-view-controller

我正在尝试重新加载提供另一个视图(BuyView)的视图(MyMatches)的UI。当BuyView被解雇时,我想重新加载MyMatches的所有视图。但是,当我尝试在“dismissViewController”完成时执行此操作时,我在“let mmvc = self.presentingViewController?as!MyMatchesViewController”行中遇到“意外发现的nil”错误。有谁知道为什么会发生这种情况,或者是否有更简单的方法来完成我想要做的事情?以下发布的代码可在BuyViewController中找到:

func itemBought() {
    print("Confirm tapped!")
    BoughtController.globalController.sendSellerNotification(seller, match: match)
    BoughtController.globalController.updateBuyer(self.item, buyer: LocalUser.user, match: self.match)
    BoughtController.globalController.updateMarket(self.item, match: self.match)
    BoughtController.globalController.updateSeller(self.item, seller: seller, soldPrice: self.match.matchedPrice)


    self.cancel = false

    if self.fromInfo == true {

        self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
    }


    else {
        self.dismissViewControllerAnimated(true) {
            let mmvc = self.presentingViewController as! MyMatchesViewController
            mmvc.setupMatchesScrollContent()
        }
    }

}

1 个答案:

答案 0 :(得分:0)

可能,dismissViewControllerAnimated()块在视图控制器被关闭后运行,因此self.presentingViewController已更改?也许。更安全使用:

else {
   let mmvc = self.presentingViewController as! MyMatchesViewController              
   self.dismissViewControllerAnimated(true) {
     mmvc.setupMatchesScrollContent()
   }
}

你在哪里捕获' mmvc在尾随闭包中使用它之前。