gpt4 book ai didi

ios - 如何连续改变多个 UITextField 中的多个文本值?

转载 作者:行者123 更新时间:2023-11-30 13:40:01 25 4
gpt4 key购买 nike

我正在寻找一种更有效的方法,只需单击一次 UIButton 即可将我的报价更改为各种 UITextFields(在本例中为 donatorView1、donorView2 等)。单击“随机化”按钮将产生一系列引号,但是,使用这组引号,每个字段只能获得一个引号,但每个框中都需要各种引号(即,donorView1 将具有字母 DQ、A、DP、DR而donorView2可以有A、C、B、DP)。我对这段代码非常陌生——因为这是我第一次编写 iOS 应用程序——所以如果可能的话,请尝试用更简单的术语进行解释!每个文本字段至少需要 6 个随机字母。

@IBOutlet weak var patientView: UITextField!

@IBAction func patientFunction(sender: UIButton!) {
}
@IBOutlet weak var donorView1: UITextField!

@IBOutlet weak var donorView2: UITextField!

@IBOutlet weak var donorView3: UITextField!

@IBOutlet weak var donorView4: UITextField!

@IBOutlet weak var donorView5: UITextField!

@IBOutlet weak var donorView6: UITextField!

@IBOutlet weak var donorView7: UITextField!

@IBOutlet weak var donorView8: UITextField!

@IBOutlet weak var Randomize: UIButton!

@IBAction func Randomize(sender: UIButton!) {

let quoteArray1 = [" -A,", " -B,", " -C,", " -DR,", " -DQ, ", " -DP,"]
self.patientView.text = quoteArray1 [Int(arc4random_uniform(UInt32(quoteArray1.count)))] + quoteArray1[Int(arc4random_uniform(UInt32(quoteArray1.count)))] + quoteArray1[Int(arc4random_uniform(UInt32(quoteArray1.count)))]
self.donorView1.text = quoteArray1[Int(arc4random_uniform(UInt32(quoteArray1.count)))] + quoteArray1[Int(arc4random_uniform(UInt32(quoteArray1.count)))] + quoteArray1[Int(arc4random_uniform(UInt32(quoteArray1.count)))]
self.donorView1.text = quoteArray1[Int(arc4random_uniform(UInt32(quoteArray1.count)))] + quoteArray1[Int(arc4random_uniform(UInt32(quoteArray1.count)))] + quoteArray1[Int(arc4random_uniform(UInt32(quoteArray1.count)))]
self.donorView2.text = quoteArray1[Int(arc4random_uniform(UInt32(quoteArray1.count)))] + quoteArray1[Int(arc4random_uniform(UInt32(quoteArray1.count)))] + quoteArray1[Int(arc4random_uniform(UInt32(quoteArray1.count)))]
self.donorView3.text = quoteArray1[Int(arc4random_uniform(UInt32(quoteArray1.count)))] + quoteArray1[Int(arc4random_uniform(UInt32(quoteArray1.count)))] + quoteArray1[Int(arc4random_uniform(UInt32(quoteArray1.count)))]
self.donorView4.text = quoteArray1[Int(arc4random_uniform(UInt32(quoteArray1.count)))] + quoteArray1[Int(arc4random_uniform(UInt32(quoteArray1.count)))] + quoteArray1[Int(arc4random_uniform(UInt32(quoteArray1.count)))]
self.donorView5.text = quoteArray1[Int(arc4random_uniform(UInt32(quoteArray1.count)))] + quoteArray1[Int(arc4random_uniform(UInt32(quoteArray1.count)))] + quoteArray1[Int(arc4random_uniform(UInt32(quoteArray1.count)))]
self.donorView6.text = quoteArray1[Int(arc4random_uniform(UInt32(quoteArray1.count)))] + quoteArray1[Int(arc4random_uniform(UInt32(quoteArray1.count)))] + quoteArray1[Int(arc4random_uniform(UInt32(quoteArray1.count)))]
self.donorView7.text = quoteArray1[Int(arc4random_uniform(UInt32(quoteArray1.count)))] + quoteArray1[Int(arc4random_uniform(UInt32(quoteArray1.count)))] + quoteArray1[Int(arc4random_uniform(UInt32(quoteArray1.count)))]
self.donorView8.text = quoteArray1[Int(arc4random_uniform(UInt32(quoteArray1.count)))] + quoteArray1[Int(arc4random_uniform(UInt32(quoteArray1.count)))] + quoteArray1[Int(arc4random_uniform(UInt32(quoteArray1.count)))]
}

override func viewDidLoad() {

super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

最佳答案

let textFields: [UITextField] = [self.patientView, self.donorView1, 2, 3, etc.] 
let quoteArray1 = [" -A,", " -B,", " -C,", " -DR,", " -DQ, ", " -DP,"]
let randomRange = UInt32(quoteArray1.count)
for textField in textFields {
textField.text = quoteArray1 [Int(arc4random_uniform(randomRange))] +
quoteArray1[Int(arc4random_uniform(randomRange))] +
quoteArray1[Int(arc4random_uniform(randomRange))]
}

只是关于风格的注释,从长远来看,这将节省您的时间,将您的 socket 和操作保留在自己的部分中,并消除 socket 之间的间距...:)

更新:

如果您需要在所有文本字段上以不同的顺序显示每个数组元素,您应该随机打乱数组,并使用每个数组元素的串联来设置文本。在设置每个标签之前对数组进行打乱。

检查这个Randomly shuffle a Swift array

关于ios - 如何连续改变多个 UITextField 中的多个文本值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35680975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com