Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

swift - 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 ?

question from:https://stackoverflow.com/questions/65940036/swiftui-is-it-possible-to-invoke-the-function-and-change-view-from-other-page

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...