0

I'm trying to add a FittedSheet (Repo) to my app, and in their Usage explanation, they just have this chunk of code with no further explanation regarding where to put it.

let controller = MyViewController()

let sheetController = SheetViewController(controller: controller)

// It is important to set animated to false or it behaves weird currently
self.present(sheetController, animated: false, completion: nil) 

I want to know where that code is supposed to go so I can include the basic version of a FittedSheet in my app. Any input would be greatly appreciated, I'm new to swift and have all the backend data in my app working but am struggling to display it.

rst123
  • 3
  • 1
  • The code you have there simply will create a new instance of `MyViewController`, add it to your `sheetController` object and then you're telling the view controller or class that is calling `present` to display it on screen. This code should be used when you want to transition to a new screen or "view". Wherever in your application that you'd like to open the new screen you can paste this code in. – KSigWyatt Jun 13 '19 at 02:32
  • Thanks, I'm starting to piece it together. I can paste that into my current ViewController named `ViewController` at the moment I want the FittedSheet to come up. I'm still not sure where I define the data I want to display in the FittedSheet? I know SheetViewController is a defined class in the pod, so I'm assuming I don't need to define anything w that name... – rst123 Jun 13 '19 at 03:33

1 Answers1

1

MyViewController() is a controller name whose data need to be present using the FittedSheet library. Let me take an example.

In the below example, i create customSheetController in my project. I design UI in storyboard and create a swift class for same.

customSheetController storyboard UI

Below is customSheetController swift class syntax

class customSheetController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
}

In some another viewcontroller class, open that sheet on collection view item click.

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        if let sheetView = storyboard.instantiateViewController(withIdentifier: "customSheetController") as? customSheetController {
           let sheetController = SheetViewController(controller: sheetView)

            // It is important to set animated to false or it behaves weird currently
            self.present(sheetController, animated: false, completion: nil)
        }
    } 

In the above example i opened a sheet on collection view item click, you can add above code on button event or any event from where you want to open that sheet.

another example

Let me take another example, suppose you have two viewcontroller class in your project lets say A and B. You want to present A controller over B controller. Then you need to modify the code as below.

In B controller class, suppose you want to present A controller on button click then you can write below code on the button click event

let controller = A()

let sheetController = SheetViewController(controller: controller)

self.present(sheetController, animated: false, completion: nil)
  • Is that second chunk of code in `customSheetController` or `MyViewController`? What needs to be included in MyViewController in order to make it work? – rst123 Jun 14 '19 at 02:24
  • I edit my answer, In the above example, I'm not using MyViewController anywhere. In the above code, I'm presenting customSheetController using FittedSheet. There are only two classes (1) customSheetController class that i presented using FittedSheet (2) viewcontroller class on which i added FittedSheet code on collection view item click. – Kuldeep Chander Jun 14 '19 at 09:57