Shanely
Shanely

Reputation: 123

SwiftUI : Display return value from a function to another view

I have to generate a random string on a button click (in the first view) and then display the string on a text (in the second view) but right now I'm stuck at toggling the second view with the text. Does someone know how I can bind the return value from the func and display it to the second view?

this is the code for random string (credit: Generate random alphanumeric string in Swift)

class randomString: ObservableObject{ 
  @Published var showDisplay: Bool = false
  @Published var s = ""

  func randomString(of length: Int) -> String { // return `String`
      let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
      var s = ""
      for _ in 0 ..< length {
          s.append(letters.randomElement()!)
          print(s)
        
          goToDisplay()
      }
      return s
  }

  func goToDisplay(){
    
      self.showDisplay.toggle()
  }
}

In the Content View I call the function from button click;

struct ContentView: View {

   @StateObject var stringData = randomString()

   var body: some View {
    
       VStack{
           Text("Generate String")
               .font(.title)
               .fontWeight(.bold)
               .foregroundColor(.primary)
               .padding()
        
           Button(action: {
               //self.showDisplay.toggle()
               stringData.randomString(of: 5)
           }, label: {
               Text("Next Page")
                   .font(.title2)
                   .fontWeight(.light)
                   .foregroundColor(Color.white)
                   .scaledToFit()
                   .frame(maxWidth: .infinity)
                   .frame(height: 60, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
                   .background(Color.orange)
                   .cornerRadius(20)
                   .padding(.all,10)


           })
           .padding(.bottom, 40)
       }
       .fullScreenCover(isPresented: $stringData.goToDisplay, content: {
           DisplayView()
       })

    }
}

And Second View looks like this for now

struct DisplayView: View {

   @StateObject var stringData = randomString()
   @Environment(\.presentationMode) var presentationMode

   var body: some View {
       Text("Random String : \(stringData.s)")
           .font(.title)
           .fontWeight(.bold)
           .foregroundColor(.primary)
           .padding()
    
       Spacer()
    
       Button(action: {
           presentationMode.wrappedValue.dismiss()
       }, label: {
          Text("Close")
               .font(.title2)
               .fontWeight(.light)
               .foregroundColor(Color.white)
               .scaledToFit()
               .frame(width: UIScreen.main.bounds.width - 100)
               .frame(height: 50)
               .scaledToFit()
               .background(Color.orange)
               .cornerRadius(12)
               .padding(.bottom)
       })
       .padding(.horizontal)
   }
}

Upvotes: 1

Views: 1832

Answers (1)

Raja Kishan
Raja Kishan

Reputation: 18914

Just change your flow.

class RandomString: ObservableObject{
    
    @Published var s: String? = ""
    
    func randomString(of length: Int) { // return `String`
        let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
        var s = ""
        for _ in 0 ..< length {
            s.append(letters.randomElement()!)
            print(s)
        }
        self.s = s
    }
}

struct ContentViewSecond: View {
    
    @StateObject var stringData = RandomString()
    @State var showDisplay = false
    var body: some View {
        
        VStack{
            Text("Generate String")
                .font(.title)
                .fontWeight(.bold)
                .foregroundColor(.primary)
                .padding()
            
            Button(action: {
                self.showDisplay.toggle()
                stringData.randomString(of: 5)
            }, label: {
                Text("Next Page")
                    .font(.title2)
                    .fontWeight(.light)
                    .foregroundColor(Color.white)
                    .scaledToFit()
                    .frame(maxWidth: .infinity)
                    .frame(height: 60, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
                    .background(Color.orange)
                    .cornerRadius(20)
                    .padding(.all,10)
                
                
            })
            .padding(.bottom, 40)
        }
        .fullScreenCover(isPresented: $showDisplay) {
            DisplayView(stringData: stringData)
        }
    }
}

struct DisplayView: View {
    
    @ObservedObject var stringData: RandomString
    @Environment(\.presentationMode) var presentationMode

    var body: some View {
        Text("Random String : \(stringData.s ?? "")")
            .font(.title)
            .fontWeight(.bold)
            .foregroundColor(.primary)
            .padding()
     
        Spacer()
     
        Button(action: {
            presentationMode.wrappedValue.dismiss()
        }, label: {
           Text("Close")
                .font(.title2)
                .fontWeight(.light)
                .foregroundColor(Color.white)
                .scaledToFit()
                .frame(width: UIScreen.main.bounds.width - 100)
                .frame(height: 50)
                .scaledToFit()
                .background(Color.orange)
                .cornerRadius(12)
                .padding(.bottom)
        })
        .padding(.horizontal)
    }
 }

Upvotes: 1

Related Questions