0

Here it's my code.

struct FirstPage: View {
    
    var body: some View {
        VStack{
            
            NavigationView {
                VStack{
                    
                    Text("First Page")
                        .bold()
                    
                    NavigationLink(destination: SecondPage()) {
                                    
                        Image(systemName:"arrowshape.turn.up.right.circle")
                    }
                }
            }
            
            if (isVisible()) {
                Image(systemName:"rhombus.fill")
                    .frame(width: 100, height: 100, alignment: .center)
            }

        }
    }
}

func isVisible() -> Bool {
    let result = Bool.random()
    
    print("result", result)
    return result
}

What I'd like to do is calling the global func isVisible() from SecondPage and change the visibility of Image(systemName:"rhombus.fill"). Is it possible to do that ?

SecondPage would be like below.

struct SecondPage: View {
   
   
   var body: some View {
       VStack{
           NavigationView {
               VStack {
                   Text("Second Page")
                       .bold()
                   Button(action: {
                       
                   }){
                       Text("Click here")
                   }
               }
           }
       }
   }
}

I want to invoke the isVisible() and change the visibility of Image when I tap the Button of SecondPage.

Does anyone know how to do that ?

1 Answer 1

2

You don't need to call the function (using the global function is not a good idea.). Just use @State and @Binding Like this

struct FirstPage: View {
    
    @State private var isVisible: Bool = false
    
    var body: some View {
        VStack{
            
            NavigationView {
                VStack{
                    
                    Text("First Page")
                        .bold()
                    
                    NavigationLink(destination: SecondPage(isVisible: $isVisible)) {
                        
                        Image(systemName:"arrowshape.turn.up.right.circle")
                    }
                }
            }
            
            if isVisible {
                Image(systemName:"rhombus.fill")
                    .frame(width: 100, height: 100, alignment: .center)
            }
            
        }
    }
}

struct SecondPage: View {
    
    @Binding var isVisible: Bool
    
    var body: some View {
        VStack{
            NavigationView {
                VStack {
                    Text("Second Page")
                        .bold()
                    Button(action: {
                        isVisible = Bool.random()
                    }){
                        Text("Click here")
                    }
                }
            }
        }
    }
}

If you still need to use the global function then you need to create a static shared Observable class.

Here is an example:

Static shared class

class GlobalClass: ObservableObject {
    
    static var shared = GlobalClass()
    
    @Published var isVisibleVar: Bool = false
    
    func isVisible() {
        let result = Bool.random()
        print("result", result)
        isVisibleVar = result
    }
}

Views

struct SecondPage: View {
    var body: some View {
        VStack{
            NavigationView {
                VStack {
                    Text("Second Page")
                        .bold()
                    Button(action: {
                        GlobalClass.shared.isVisible()
                        
                        /**
                         Or you can use
                         GlobalClass.shared.isVisibleVar = Bool.random()
                         */
                        
                    }){
                        Text("Click here")
                    }
                }
            }
        }
    }
}


struct FirstPage: View {
    
    @ObservedObject private var globalClass = GlobalClass.shared
    
    var body: some View {
        VStack{
            NavigationView {
                VStack{
                    Text("First Page")
                        .bold()
                    
                    NavigationLink(destination: SecondPage()) {
                        Image(systemName:"arrowshape.turn.up.right.circle")
                    }
                }
            }
            
            if globalClass.isVisibleVar {
                Image(systemName:"rhombus.fill")
                    .frame(width: 100, height: 100, alignment: .center)
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer !! That' s so helpful ! As for your second answer you use singleton object, but what if it's implemented as EnvironmentObject ? Are those behave like same way ?
Remove static var shared = GlobalClass() from GlobalClass and declare with EnvironmentObject like this in each page. @EnvironmentObject private var globalClass :GlobalClass Then add .environmentObject(GlobalClass()) to FirstPage() at SceneDelegate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.