Hello, I'm new to SwiftUI and right now I'm trying to create one simple Application by using @State, @ObservedObject, and @Environment.
For some reason, I got error "Cannot preview in this file" after I tried to add Environment object. The canvas is not working, but I can actually run the application in a simulator and actual device without any problem. Here is how I added my environment object.
SceneDelegate:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
let channelData = ChannelData()
// Create the SwiftUI view that provides the window contents.
let contentView = ContentView().environmentObject(channelData)
// Use a UIHostingController as window root view controller.
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible(
}
}
ChannelData class:
import Combine
final class ChannelData: ObservableObject{
@Published var channelName = "YouTube Channel"
}
ContentView:
import SwiftUI
struct ContentView: View {
@State private var showingSecondVC = false
@ObservedObject var videoIdea = VideoIdea()
@EnvironmentObject var channelData: ChannelData
var body: some View {
NavigationView{
VStack(alignment: .leading){
Text(videoIdea.title)
.font(.title)
Text(videoIdea.contentIdea)
.font(.subheadline)
Divider()
NavigationLink(destination: ChannelView()){
Text("Add Channel")
}
Button(action: {
self.showingSecondVC.toggle()
}){
Text("Add New Idea")
}.sheet(isPresented: $showingSecondVC){
SecondView(videoTitle: self.$videoIdea.title, videoContent: self.$videoIdea.contentIdea).environmentObject(self.channelData)
}
Spacer()
}.padding()
.navigationBarTitle(channelData.channelName)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
SecondView:
import SwiftUI
struct SecondView: View {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
@Binding var videoTitle: String
@Binding var videoContent: String
@EnvironmentObject var channelData:ChannelData
var body: some View {
NavigationView {
VStack(alignment: .leading){
TextField("Video title", text: $videoTitle)
TextField("Video Content", text: $videoContent)
Divider()
Button(action: {
self.presentationMode.wrappedValue.dismiss()
}){
Text("Dismiss this VC")
}
Spacer()
}.padding()
.navigationBarTitle(channelData.channelName)
}
}
}
ChannelView:
import SwiftUI
struct ChannelView: View {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
@State private var channelName = "My Channel"
@EnvironmentObject var channelData: ChannelData
var body: some View {
NavigationView{
VStack(alignment: .leading){
TextField("Channel Name", text: $channelName)
Divider()
Button(action: {
self.presentationMode.wrappedValue.dismiss()
}){
Text("Dismiss this VS")
}
Spacer()
}.padding()
.navigationBarTitle(channelData.channelName)
}
}
}