open a New View from a item in a contextual menu

Bonjour as the title says:

I have a contextual menu in my app, using swift, I want to open a New View from a item in a contextual menu

when I click on the like to go to MainView, nothings happens Please Help me I am very new in IOS programmation

here is my code so far:

                 .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    Menu {
                            
                        NavigationLink(destination:MainView()){
                            Label("MainView", systemImage: "house")
                        }
                        
                        
                        Button(action: {
                            // Show modal text for copyright
                        }) {
                            Label("Modal Text", systemImage: "text.book.closed")
                        }
                        
                        Button(action: {
                            // Go to HelpView
                        }) {
                            Label("HelpView", systemImage: "questionmark.circle")
                        }
                    } label: {
                        Image(systemName: "house")
                    }
                }
            }

Thanks for helping me I have been fighting with this for hours

David

Could you please provide more context? Maybe a screenshot of what you would like to accomplish?

What I can say for now is that you didn’t write anything into the “HelpView” button action. Therefore, it doesn’t do anything.

To learn more about SwiftUI, you might be interested in the following course : https://developer.apple.com/tutorials/app-dev-training

If I understand correctly, you should do this:

  • create State vars
@State private var showCopyright = false
  • Set it in the Action
                        Button(action: {
                            showCopyright = true // Show modal text for copyright
                        }) {
                            Label("Modal Text", systemImage: "text.book.closed")
                        }
  • Use it to show the view, in the main part of the body (not in the Menu nor ToolBarItem)
if showCopyright {
   CopyrightView()  // Or whatever you call it ; it could also simply be a Text()
}
  • You should have a close button in CopyrightView to hide:
                        Button(action: {
                            showCopyright = false // hide
                        }) {
                            Label("Close", systemImage: "") // Select the appropriate image
                        }

Bonjour Here is my code in context

  import SwiftUI

 struct Bible: View {
@State private var selectedTab = 0

var body: some View {
    
    NavigationView {
        
                        TabView(selection: $selectedTab) {
                View1()
                    .tabItem {
                        Image(systemName: "book")
                        Text("La vue 1")
                    }
                    .tag(0)
                
                    View2()
                   .tabItem {
                    Image(systemName: "heart")
                    Text("La vue 2")
                }
                .tag(1)
                
            }
            //.navigationBarTitleDisplayMode(.inline)
            .navigationBarTitle("Ma page")
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    Menu {
                        
                        NavigationLink(destination:MainView()){
                         Label("MainView", systemImage: "house")
                          }
                        
                        Button(action: {

                            CopyrightView()
                            
                        }) {
                            Label("Copyright", systemImage: "text.book.closed")
                        }
                        
                        Button(action: {
                            NavigationLink(destination:HelpView()){
                             Label("Help", systemImage: "questionmark.circle")
                            }
                            
                        }) {
                        }
                    } label: {
                        Image(systemName: "house")
                    }
                }
            }
            
             }

         }

        }

I try 3 different way to call the view, MainView(), CopyrightView() and HelpView() without success I use a tab view to call View1 and View 2

The menu is shown in View1 and View2 and opens well, but it is not possible to open the 3 views from there

thank you for your help

blessings

David

This is wrong, and should not compile (action cannot call a View directly):

                        Button(action: {
                            CopyrightView()
                        }) {
                            Label("Copyright", systemImage: "text.book.closed")
                        }

You must do what I explained in previous post (refer to it for complete explanation):

                        Button(action: {
                            showCopyright = true // Show modal text for copyright
                        }) {
                            Label("Modal Text", systemImage: "text.book.closed")
                        }

I did exactly as you indicated without success

The code is still wrong?

 import SwiftUI

 struct Bible: View {
 @State private var selectedTab = 0
 @State private var showCopyright = false

var body: some View {

NavigationView {
    
                    TabView(selection: $selectedTab) {
            View1()
                .tabItem {
                    Image(systemName: "book")
                    Text("La vue 1")
                }
                .tag(0)
            
                View2()
               .tabItem {
                Image(systemName: "heart")
                Text("La vue 2")
            }
            .tag(1)
            
        }
        //.navigationBarTitleDisplayMode(.inline)
        .navigationBarTitle("Ma page")
        .toolbar {
            ToolbarItem(placement: .navigationBarTrailing) {
                Menu {
                    
                    NavigationLink(destination:MainView()){
                     Label("MainView", systemImage: "house")
                      }
                    
                    Button(action: {

          showCopyright = true // Show modal text for copyright       
                 
                    }) {
                        Label("Copyright", systemImage: "text.book.closed")
                    }
                    
                    Button(action: {
                        NavigationLink(destination:HelpView()){
                         Label("Help", systemImage: "questionmark.circle")
                        }
                        
                    }) {
                    }
                } label: {
                    Image(systemName: "house")
                }
            }
        }
        
         }

     }

 if showCopyright {
 CopyrightView()  // Or whatever you call it ; it could also simply be a Text()
  }


    }

I did exactly as you indicated without success

That's not a very useful feedback. Please explain in detail what you get and what you expect.

Error here: this must be INSIDE body

 if showCopyright {
      CopyrightView()  // Or whatever you call it ; it could also simply be a Text()
  }

Did you define CopyrightView ?

I tested the following code (adding some dummy views definitions that were missing) and it works (except that Button action cannot be a NavigationLink)

                        Button(action: {
                            NavigationLink(destination:HelpView()){
                                Label("Help", systemImage: "questionmark.circle")
                            }
struct View1: View {
    var body: some View {
        
        Text("View1")
    }
}

struct View2: View {
    var body: some View {
        
        Text("View2")
    }
}

struct MainView: View {
    var body: some View {
        
        Text("MainView")
    }
}

struct HelpView: View {
    var body: some View {
        
        Text("HelpView")
    }
}

struct CopyrightView: View {
    var body: some View {
        
        Text("CopyrightView")
    }
}

struct Bible: View {
    @State private var selectedTab = 0
    @State private var showCopyright = false

    var body: some View {
        
        NavigationView {
            
            TabView(selection: $selectedTab) {
                View1()
                    .tabItem {
                        Image(systemName: "book")
                        Text("La vue 1")
                    }
                    .tag(0)
                
                View2()
                    .tabItem {
                        Image(systemName: "heart")
                        Text("La vue 2")
                    }
                    .tag(1)
                
            }
            //.navigationBarTitleDisplayMode(.inline)
            .navigationBarTitle("Ma page")
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    Menu {
                        
                        NavigationLink(destination:MainView()){
                            Label("MainView", systemImage: "house")
                        }
                        
                        Button(action: {
                            showCopyright = true // Show modal text for copyright
                        }) {
                            Label("Copyright", systemImage: "text.book.closed")
                        }
                        
                        Button(action: {
                            print("HELP, you have to change this")
//                            NavigationLink(destination:HelpView()){
//                                Label("Help", systemImage: "questionmark.circle")
//                            }
                            
                        }) {
                            Text("HELP")
                        }
                    } label: {
                        Image(systemName: "house")
                    }
                }
            }
            
        }
        
        if showCopyright {  // MUST BE INSIDE body
            CopyrightView()  // Or whatever you call it ; it could also simply be a Text()
        }
        
    }
    
}

Ok thanks

I think we are not far from the result I expected

this is what I want to achieve?

when the app start it opens a MainView()

here is the MainView code

         import SwiftUI

 struct MainView: View {
var body: some View {
    NavigationView {
        VStack(spacing: 10) {
            Spacer()
            
            Text("Example en panne")
                .font(.system(size: 40))
                
            
                NavigationLink(destination: View1()
                    .navigationBarBackButtonHidden(true)
                ) {
                    VStack {
                        Image(systemName: "book")
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                            .frame(width: 140, height: 140)
                        
                        Text("Ma vue 1")
                            .font(.headline)
                    }
                }
                
        
            
            Spacer()

            
        }
       }
    }
 }

  struct MainView_Previews: PreviewProvider {
   static var previews: some View {
    MainView()
    }
 }






   

from the MainView I call View1 or View2

Here is the code for View1

import SwiftUI

  struct View1: View {
    @State private var selectedTab = 0
   @State private var showCopyright = false

  var body: some View {
    
    
    if showCopyright {  // MUST BE INSIDE body
        MainView()  // Or whatever you call it ; it could also simply be a Text()
    }
    
    NavigationView {
        
                 TabView(selection: $selectedTab) {
                         maPage1()
                     
                    .tabItem {
                        Image(systemName: "book")
                        Text("Page 1")
                    }
                    .tag(0)
                
                    maPage2()
        
                .tabItem {
                    Image(systemName: "heart")
                    Text("Page 2")
                }
                .tag(1)
               
                
                
                
            }
            //.navigationBarTitleDisplayMode(.inline)
            .navigationBarTitle("truc")
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    Menu {
                            
                        Button(action: {
                            showCopyright = true // Show modal text for copyright
                        }) {
                            Label("Copyright", systemImage: "text.book.closed")
                        }
                        
                        Button(action: {
                            // Go to HelpView
                        }) {
                            Label("HelpView", systemImage: "questionmark.circle")
                        }
                    } label: {
                        Image(systemName: "house")
                    }
                                 }
                             }
        
                          
                         }
                         
                        
    
                         
                     }
                     
             
         }

and the CopyrightView is as such:

  import SwiftUI

 struct CopyrightView: View {
  var body: some View {
    Text("Here is the Copyright text en some details about the App")
    }
   }

   struct CopyrightView_Previews: PreviewProvider {
   static var previews: some View {
    CopyrightView()
      }
    }

  

nothing fancy

the behavior I want:

when the app opens, It shows Mainview when I click on the icon for View1 it opens View1 till now all is well

from View1 I want to click on the "house" icon in the contextual Menu to open MainView the problem :

from the contextual Menu, it opens Mainview() now but but only on half of the screen, the other part of the screen still show View1

Before I had nothing when I clicked on the icon for MainView in the contextual menu, now things get better I hope I explain better

Thanks

Bonjour Thank you again for your help

That was easier than expected... After 2 months of hard reading on iOS, swiftui, I think my brain got it and I starting to believe that iOS dev is not unreachable

So the solution for me: use a popover, a sheet view or simply a navigationlink When you don't know, you don't know... we have all been newbies...

example:

inline-code

                  ToolbarItem(placement: .navigationBarTrailing) {
                    Menu {
                        NavigationLink(destination: MainView()
                            .navigationBarBackButtonHidden(true)
                        ) {
                            Label("Accueil", systemImage: "homekit")                            }
                    /// rest of the code 

Merci beaucoup for your help

open a New View from a item in a contextual menu
 
 
Q