Creating nav link error in Swift UI/ Xcode - swiftui

I am trying to add a Navigation view that goes from my ContentView to my list items inside of my SectionView and then goes to my DetailView...i'm getting an error and i'm not sure why? im trying to nav link SelectionView( codeName: "Y")
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
List {
NavigationLink("Y", destination:codeName.
(model: .codeName))
List (models) { model in
SelectionView(codeName: model.codeName)
}
}
}
#if DEBUG
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.previewLayout(.device)
.preferredColorScheme(.dark)
.previewDevice("iPhone 13 Pro Max")
}
}
#endif
}
}

The destination also should be a view.
struct ContentView: View {
var body: some View {
NavigationView {
List {
ForEach(0 ..< 5) { item in
NavigationLink(
destination: Text("Destination \(item)"),
label: {
Text("Hello, world! \(item)")
.padding()
})
}
}
}
}
}

Related

How can I Hide TabBar in specific Views, in iOS 15 using SwiftUI

I need my TabBar to disappear if I click on a NavigationLink.
I know you can achieve that in iOS 14 with the following code:
NavigationView{
TabView{
View1().tabItem {
Image(systemName: "house.fill")
Text("Home")
}
}
}
And View1:
struct View1: some View {
var body: some View {
NavigationView{
NavigationLink(destination: Text("New Page without the Tabbar")) {
Text("Link")
}
}
}
}
But somehow this don't works in iOS 15...
Are there any other workarounds?
You could try using only one NavigationView, like in this example:
import SwiftUI
#main
struct TestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct ContentView: View {
var body: some View {
NavigationView {
TabView {
View1().tabItem {
Image(systemName: "house.fill")
Text("Home")
}
}
}
}
}
struct View1: View {
var body: some View {
// ---> here no NavigationView
NavigationLink(destination: Text("New Page without the Tabbar")) {
Text("Link")
}
}
}

NavigationLink in DisclosureGroup is not working when clicked

I'm trying to make a NavigationLink to "appleView()" swift file in a DisclosureGroup with the title "fruits".
But the NavigationLink is not working when clicked.
I don't know how to fix it.
I would appreciate it if anybody helped.
import SwiftUI
struct ContentView: View {
#State private var fruitsExpanded: Bool = false
var body: some View {
Form{
DisclosureGroup(
isExpanded: $fruitsExpanded,
content: {
List() {
NavigationLink(
destination: AppleView(),
label: {
Text("Apple")
})
Text("Banana")
}
},
label: { Text("Fruits") }
)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

Why do I exit my TabView when navigating to a child view?

Question. Why do I exit my TabView when navigating to a view that is a child of a TabView? I hope I am being clear, but the code below is ready to be copy and pasted and notice how when I navigate to UnrelatedView I exit my tabView...
Additional context: The root view ContentView in this case is embedded inside of a NavigationView in the App.swift file
import SwiftUI
struct ContentView: View {
var body: some View {
TabView {
ViewA()
.tabItem {
Text("A")
}
ViewB()
.tabItem {
Text("B")
}
ViewC()
.tabItem {
Text("C")
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct ViewA: View {
var body: some View {
NavigationLink(destination: UnrelatedView()) {
Text("ViewA")
}
}
}
struct ViewB: View {
var body: some View {
Text("ViewB")
}
}
struct ViewC: View {
var body: some View {
Text("ViewC")
}
}
struct UnrelatedView: View {
var body: some View {
NavigationView {
Text("Unrelated View")
}
}
}
You need to wrap your navigation in NavigationViews.
struct ViewA: View {
var body: some View {
NavigationView {
NavigationLink(destination: UnrelatedView()) {
Text("ViewA")
}
}
}
}

How to get a value as to which view is selected in TabView?

I'm currently developing an application using SwiftUI.
This app has 2 Views controlled a Tab View.
I want to get a value as to which view is selected in TabView.
Is there any way to do that?
ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
TabView {
FirstView()
.tabItem {
Text("First")
}.tag(1)
SecondView()
.tabItem {
Text("Second")
}.tag(2)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
FirstView.swift
import SwiftUI
struct FirstView: View {
var body: some View {
Text("FirstView")
}
}
struct FirstView_Previews: PreviewProvider {
static var previews: some View {
FirstView()
}
}
SecondView.swift
import SwiftUI
struct SecondView: View {
var body: some View {
Text("SecondView")
}
}
struct SecondView_Previews: PreviewProvider {
static var previews: some View {
SecondView()
}
}
Xcode: Version 11.7
Swift: Swift 5
You need to use selection, like below
Note: selection should be same type as used for tags (and use corresponding values from tags to select specific tab programmatically)
struct ContentView: View {
#State private var selectedTab = 1 // default selection
var body: some View {
TabView(selection: $selectedTab) { // << here !!
FirstView()
.tabItem {
Text("First")
}.tag(1)
SecondView()
.tabItem {
Text("Second")
}.tag(2)
}
}
}

HStack NavigationLink in Form tappable area small

I would like to be able to tap the entire row in this form to navigate to the next view; what am I doing wrong? It only allows me to navigate if I tap the symbol at the end.
This code should compile and run in your simulator.
Thanks.
import SwiftUI
struct SwiftUIView: View {
let selectedTags = ["A", "B", "C"]
var body: some View {
NavigationView {
Form {
NavigationLink(destination: DetailView()) {
ScrollView(.horizontal) {
HStack {
ForEach(selectedTags, id: \.self) { tag in
Text(tag)
}
}
}
}
}
}
}
}
struct DetailView: View {
var body: some View {
Text("Welcome")
}
}
struct SwiftUIView_Previews: PreviewProvider {
static var previews: some View {
SwiftUIView()
}
}
copy - paste - run
import SwiftUI
struct ContentView: View {
let selectedTags = ["A", "B", "C"]
#State var active = false
var body: some View {
NavigationView {
Form {
NavigationLink( destination: DetailView(), isActive: $active) {
ScrollView(.horizontal) {
HStack {
ForEach(selectedTags, id: \.self) { tag in
Text(tag)
}
}
}.onTapGesture {
self.active.toggle()
}
}
}
}
}
}
struct DetailView: View {
var body: some View {
Text("Welcome")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}