How do I access a view from a different file?

I have 2 files that I want to work together or something.

The first is the main file: ContentView.swift. The second is the file where my other view is: KeysView.swift.

This is what KeysView contains:

Code Block
struct KeysView: View {
    var body: some View {
HStack {
Button(action: {
}) {
Text("1")
.padding()
.background(Color.black)
.clipShape(/*@START_MENU_TOKEN@*/Circle()/*@END_MENU_TOKEN@*/)
}
Button(action: {
}) {
Text("2")
.padding()
.background(Color.black)
.clipShape(/*@START_MENU_TOKEN@*/Circle()/*@END_MENU_TOKEN@*/)
}
Button(action: {
}) {
Text("3")
.padding()
.background(Color.black)
.clipShape(/*@START_MENU_TOKEN@*/Circle()/*@END_MENU_TOKEN@*/)
}
}
HStack {
Button(action: {
}) {
Text("4")
.padding()
.background(Color.black)
.clipShape(/*@START_MENU_TOKEN@*/Circle()/*@END_MENU_TOKEN@*/)
}
Button(action: {
}) {
Text("5")
.padding()
.background(Color.black)
.clipShape(/*@START_MENU_TOKEN@*/Circle()/*@END_MENU_TOKEN@*/)
}
Button(action: {
}) {
Text("6")
.padding()
.background(Color.black)
.clipShape(/*@START_MENU_TOKEN@*/Circle()/*@END_MENU_TOKEN@*/)
}
}
HStack {
Button(action: {
}) {
Text("7")
.padding()
.background(Color.black)
.clipShape(/*@START_MENU_TOKEN@*/Circle()/*@END_MENU_TOKEN@*/)
}
Button(action: {
}) {
Text("8")
.padding()
.background(Color.black)
.clipShape(/*@START_MENU_TOKEN@*/Circle()/*@END_MENU_TOKEN@*/)
}
Button(action: {
}) {
Text("9")
.padding()
.background(Color.black)
.clipShape(/*@START_MENU_TOKEN@*/Circle()/*@END_MENU_TOKEN@*/)
}
}
    }
}

In ContentView, I'm trying to call the view using KeysView(), but I keep getting a Cannot find 'KeysView' in scope error.
May I have a look on your ContentView.swift?
@2012jacky, sure.
Code Block
//
//  ContentView.swift
//  MinimalisticCalculator
//
//  Created by Joe Mama on 10/6/20.
//
import SwiftUI
struct ContentView: View {
@State private var darkMode = false
    var body: some View {
ZStack{
if darkMode {
Color.black
.ignoresSafeArea()
} else {
}
VStack{
KeysView()
HStack {
Button(action: {
self.darkMode.toggle()
}) {
if darkMode {
Image(systemName: "moon.fill")
} else {
Image(systemName: "moon")
}
}
}
}
}
    }
}
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Accepted Answer
OK. I have a slightly look into your KeysView.swift. There is a major issue with your codes. The body should return to some view, which is one and only view type. However, you just put three HStack together, which would not work. An easy fix would be to wrap the three HStack in a single VStack, which I think is what you intended but forgot to do.

In short, you forget to wrap the three HStack in a VStack.

I tested your project, with 2 files, and it works immediately.

No problem with HStack…


Please, when you post code, use the Paste and Match Style (direct paste gives an awful presentation with a lot if extra lines).
I have edited to keep only 2 actions

Code Block
struct KeysView: View {
var body: some View {
HStack {
Button(action: {
}) {
Text("1")
.padding()
.background(Color.black)
.clipShape(/*@START_MENU_TOKEN@*/Circle()/*@END_MENU_TOKEN@*/)
}
Button(action: {
}) {
Text("9")
.padding()
.background(Color.black)
.clipShape(/*@START_MENU_TOKEN@*/Circle()/*@END_MENU_TOKEN@*/)
}
}
}
}


Code Block
import SwiftUI
struct ContentView: View {
@State private var darkMode = false
var body: some View {
ZStack{
if darkMode {
Color.black
.ignoresSafeArea()
} else {
}
VStack{
KeysView()
HStack {
Button(action: {
self.darkMode.toggle()
}) {
if darkMode {
Image(systemName: "moon.fill")
} else {
Image(systemName: "moon")
}
}
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

How do I access a view from a different file?
 
 
Q