Home > database >  Swift 5 & sheetPresentationController passing data
Swift 5 & sheetPresentationController passing data

Time:03-06

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.

Edit: If you downvote, please explain why, I have searched all of SO & Google for an answer.

CodePudding user response:

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