harunaga
harunaga

Reputation: 385

SwiftUI Is it possible to invoke the function and change view from other page?

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 ?

Upvotes: 0

Views: 543

Answers (1)

Raja Kishan
Raja Kishan

Reputation: 19014

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)
            }
        }
    }
}

Upvotes: 2

Related Questions