Swift 5 & sheetPresentationController 传递数据

Swift 5 & sheetPresentationController passing data

我正在学习 swift 并尝试将变量传递给 sheet 但是当 sheet 弹出时它并没有改变变量,有没有不同的方法这样做?

let bottomSheet = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "bottomSheet")
    
    if let sheet = bottomSheet.sheetPresentationController{
        sheet.detents = [.medium()]
        sheet.prefersGrabberVisible = true
        sheet.preferredCornerRadius = 24
        sheet.prefersGrabberVisible = true
    }

    bottomSheet.isEnabled = true
    self.present(bottomSheet, animated: true, completion: nil)

在 bottomSheet 视图控制器中我有变量

var isEnabled: Bool = false

但即使我输入 true 也总是显示为 false

这样试试,你需要做的就是将它指定为视图控制器。

let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
if let bottomSheet = mainStoryboard.instantiateViewController(withIdentifier: "bottomSheet") as? BottomSheetVC{
        if let sheet = bottomSheet.sheetPresentationController{
            sheet.detents = [.medium()]
            sheet.prefersGrabberVisible = true
            sheet.preferredCornerRadius = 24
            sheet.prefersGrabberVisible = true
        }
        
        bottomSheet.isEnabled = true
        self.present(bottomSheet, animated: true)
    }