Search code examples
xcodeswift5

Swift 5 & sheetPresentationController passing data


I am learning swift and trying to pass a variable to a sheet but it isn't changing the variable when the sheet pops up, is there a different way to do this?

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)

And in the bottomSheet view controller I have the variable

var isEnabled: Bool = false

But even though I put true it always shows as false


Solution

  • Try it like this, what you need to do is specify it as a view controller.

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