ios - 在dismissViewController回调中刷新presentingViewController的UI时意外发现nil

标签 ios swift model-view-controller

我正在尝试重新加载呈现另一个 View (BuyView) 的 View (MyMatches) 的 UI。当 BuyView 被关闭时,我想重新加载 MyMatches 的所有 View 。然而,当我尝试在“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()
        }
    }

}

最佳答案

很可能,dismissViewControllerAnimated() block 在 View Controller 被关闭后运行,因此 self.presentingViewController 发生了变化?也许。使用更安全:

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

在尾随闭包中使用 mmvc 之前“捕获”它的位置。

关于ios - 在dismissViewController回调中刷新presentingViewController的UI时意外发现nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39299657/

相关文章:

swift - 实例成员不能用于自定义类的类型

c# - 使用 MVC3 将共享项(如菜单)的逻辑放在哪里?

iOS Swift - 比较 NSDate 是否在现在和 x 天之间的最佳方法

ios - 尝试在 PickerView 上设置今天的日期时崩溃

ios - 使用 Swift 将数据写入自定义文件夹

ios - 将行插入到 UITableView 的顶部会导致行向下移动,从而导致动画效果不佳

javascript - Javascript 的 MVC 模式的最佳示例?

php - 将 Ajax/UI JS 框架(ext、jquery-ui)与 MVC PHP 框架(zend、symfony)结合起来是个好主意吗?

ios - Swift 3 核心数据 NSInternalInconsistencyException entityForName 在此模型中找不到名为 Foo 的实体

ios - NSURLSession 不发送 HTTPAdditionalHeaders 的问题