简体   繁体   中英

Show random TextField as a text

I want to make button, which pressed will show us random textfield (from 3 textfields: username, username2 or username3). Currently I have something like this, but don't know how to make it possible.

struct Test: View {

/// @State private var names : ??? - I don't know what should be there
    @State private var username: String = ""
    @State var username2: String = ""
    @State var username3: String = ""
    var body: some View {
        
        
        NavigationView {
            VStack {
                TextField("Your name", text: $username)
                TextField("Your name2", text: $username2)
                TextField("Your name3", text: $username3)
                
                Button(action: randomName) {
                    Text("draw")
                            
                        }
                    
             }
                
                Text("names.text") /// it doesn't work
                .foregroundColor(.black)
                .font(.largeTitle)
                
            }
        }
    }
    
    
    private func randomName() {
        let names = ["\(username)", "\(username2)", "\(username3)"]
    }
}

I have tried to add everything into first @State private var names, but nothing work properly. Maybe I am just trying in wrong way? Or it shouldn't be done by 'let names'? I don't know and have no idea.

first of all you just need to add randomName as State.

struct Example: View {

    @State private var username: String = ""
    @State var username2: String = ""
    @State var username3: String = ""
    @State var selectedName: String = "Initial Value"
    
    var body: some View {
        NavigationView {
            VStack {
                TextField("Your name", text: $username)
                TextField("Your name2", text: $username2)
                TextField("Your name3", text: $username3)
                
                Button(action: randomName) {
                    Text("draw")
                }
                
                Text(selectedName) 
                    .foregroundColor(.black)
                    .font(.largeTitle)
            }
                    
          }
        }
        
        private func randomName() {
            let names = ["\(username)", "\(username2)", "\(username3)"]
            
            selectedName = names[Int.random(in: 0..<names.count)]
        }
}

The username , username2 and username3 properties are already strings. No need to use string interpolation when you create your array.

Swift arrays have a handy randomElement() method. The only wrinkle is that it returns an optional - It will return nil if the array is empty, so you need to handle this. There are three ways:

  1. You can use an if let to skip the code if the result is nil
  2. You can use the nil-coalescing operator ( ?? ) to provide a default value if the result is nil
  3. You can force unwrap using ! . This is generally not a good approach, although it could work in this case because the array cannot be empty.

Option 1:

private func randomName() {
   let names = [username, username2, username3]
   if let randomName = names.randomElement() {
       selectedName = randomName
   }
}

Option 2:

private func randomName() {
   let names = [username, username2, username3]
   selectedName = names.randomElement() ?? ""
}

Option 3:

private func randomName() {
   let names = [username, username2, username3]
   selectedName = names.randomElement()!
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM