rst123
rst123

Reputation: 3

Where in my ViewController do I put initializing code?

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.

Upvotes: 0

Views: 156

Answers (1)

Kuldeep Chander
Kuldeep Chander

Reputation: 141

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)

Upvotes: 1

Related Questions