How do I update a text view?

I’ve just begun using Swift and I’m trying to show a different random number in a text view every time I click on “Random Number” that’s in a list. Right now it only shows a random number once. So I’ve tried to use .onAppear in various places in my code but I keep getting error messages. Should I be using .onAppear? Is there something else I should be using? Can anyone help me? Thank you.

if selection == "Random Number." {
{
let x = Int.random(in: 1...10)
HStack {
Text(String(x))
}
}

It is not clear to mean what you mean with the list. If "Random Number" is a button, you could try something like this:

@State var x = Int.random(in: 1...10)

var body: some View {
  List {
    Button("Random Number") {
      x = Int.random(in: 1...10)
    }
    Text("random number: \(x)")
  }
}

You can also try to use

.onChange(of: selection) {
  if selection == "Random Number" {
    x = Int.random(in: 1...10)
  }
  selection = nil 

You don't need to use selection in a List, if all you want is a button you can hit multiple times. If you want to use selection, you need to reset the selection each time.

Thank you for trying Johannes but I'm sorry I still can't work it out. This is what I'm trying to do.

mylistbox.additem ("Random Name")
mylistbox.additem ("Random Number")
mylistbox.additem ("Random Planet")
if "Random Number" then
x = randomnumber(1 to 10)
mytextbox.text = x
end if

Then the user clicks on "Random Number" from that list and they are shown a unique random number in a textbox. Please understand that I am very new to Swift.

I am trying to get my list to work and the only way I can do that is to use selection. This "selection code" is copied from other people so that I can learn Swift. So I'm using this code from you.

onChange(of: selection) {
if selection == "Random Number" {
x = Int.random(in: 1...10)
}
selection = nil
}

I have tried putting this code in many places in my code but it does not work. I added a curly bracket to your code but I am still getting errors. In Xcode's developer documentation there is two onChange examples. I am using onChange(of:perform:) no? Some website tutorials are using onChange in different areas in their code. But I don't know which area to put this code in. So can you or someone else please help me? Thank you.

Can you please share more of your code?
You are using the correct onChange() method.

Sorry for being late. This is the code. The reply box in the Swift Forum does not indent my code unfortunately.

import SwiftUI
struct ContentView: View{
@State private var selection: String?
let names = ["Something Else", "Random Number"]
var body: some View{

VStack{
List(selection: $selection){
ForEach(names, id: .self){
item in Text(item)}}
.frame(width: 200, height: 100)
.border(Color.black, width: 1)
ScrollView{
if selection == "Something Else"{
HStack{
Text("Something Else")
Spacer()}
}
if selection == "Random Number"{
HStack{
Text("?")
Spacer()}
}
}
.frame(width: 200, height: 100)
.border(Color.black, width: 1)
Spacer()
}}

}

Try the 'pre-formatted text' option for code instead of the 'blockquote' option.
Screenshot 2021-08-08 at 7.39.34 am

The selection parameter to your List doesn't work the way you are thinking it works. From the documentation page:

On iOS and tvOS, you must explicitly put the list into edit mode for the selection to apply.

Indeed, it you add an EditButton() underneath the Spacer and tap on it, you will see that you can select a row and the appropriate text shows up in the ScrollView below.

You want to do something like this instead for the List:

            List {
                ForEach(names, id: \.self) { item in
                    Text(item)
                        .onTapGesture {
                            selection = item
                        }
                }
            }

Thank you for your help. Ok Diggory I will do that. Roosterboy I am still confused. This is for a MacOS computer not an iOS or tvOS. I don't understand how the Edit button is going to select a row. I want to click on a row in the list that has an item in it such as "Random Number"

The code @roosterboy provided also works on MacOS.

As he says the selection feature provided by the List API is not what you want. What you want, is a click (or tap).

Terms of Service

Privacy Policy

Cookie Policy