Peter Jones
Peter Jones

Reputation: 71

How do I navigate to a View Controller while also using SwiftUI?

I am trying to navigate to a View Controller while currently using NavigationLink from SwiftUI. I am assuming I am not able to do this, so I am wondering how I can navigate to my View Controller in another way while still being able to click on a button from my HomeView and navigate to my ViewController.

Below is my HomeView where I want a button with the text 'Time Sheets' to navigate to my ViewController

struct HomeView: View {
    var body: some View {
        NavigationView {
        VStack {
            Image("OELogo")
                .resizable()
                .frame(width: 400, height: 300)
            
            NavigationLink(destination: ViewController()) {
                Text("Time Sheets")
                .fontWeight(.bold)
                    .frame(minWidth: 325, minHeight: 50)
                .font(.title)
                .foregroundColor(.gray)
                .padding()
                .overlay(
                    RoundedRectangle(cornerRadius: 50)
                        .stroke(Color.gray, lineWidth: 2)
                )
            }

Below is the start to my code of ViewController file that I want to navigate to

import UIKit
import KVKCalendar
import SwiftUI

final class ViewController: UIViewController {
    private var events = [Event]()
    
    private var selectDate: Date = {
        let formatter = DateFormatter()
        formatter.dateFormat = "dd.MM.yyyy"
        return formatter.date(from: "27.7.2020") ?? Date()
    }()```

Upvotes: 7

Views: 6844

Answers (1)

Christian Ray Leovido
Christian Ray Leovido

Reputation: 758

To achieve this, you would need to create a new struct that conforms to UIViewControllerRepresentable. This acts like a wrapper for UIKits UIViewController. There is a similar protocol for UIView, UIViewRepresentable.

struct YourViewControllerView: UIViewControllerRepresentable {
    func makeUIViewController(context: Context) -> ViewController {
        // this will work if you are not using Storyboards at all.
        return ViewController()
    }

    func updateUIViewController(_ uiViewController: ViewController, context: Context) {
        // update code   
    }

}

Alternatively, this struct will work if you have your ViewController inside a storyboard.

struct YourViewControllerViewWithStoryboard: UIViewControllerRepresentable {
    func makeUIViewController(context: Context) -> ViewController {
        guard let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: "ViewController") as? ViewController else {
            fatalError("ViewController not implemented in storyboard")
        }
        return viewController
    }

    func updateUIViewController(_ uiViewController: ViewController, context: Context) {
        // update code
    }
}

Remember to set the Restoration ID and Storyboard ID in the Interface builder if you are using a storyboard

Upvotes: 8

Related Questions