如何连续变换多个UITextField中的多个文本值?

时间:2016-02-28 09:46:54

标签: ios swift

我正在寻找一种方法,只需点击一次UIButton,就可以找到一种更有效的方法来将我的引号变换到各种UITextFields(在本例中为donorView1,donorView2等)。单击Randomize按钮会产生一个引号数组,但是,使用这个引号数组,每个字段只能得到一个引号,但我需要在每个框中使用不同的引号(即,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.
}

1 个答案:

答案 0 :(得分:0)

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

只是一个关于风格的说明,可以长期保存你,保持你的出口和行动在他们自己的部分,并抛弃出口之间的间距... :)

<强>更新

如果需要在所有文本字段中以各种顺序显示每个数组元素,则应随机对数组进行随机排列,并使用每个数组元素的串联设置文本。在设置每个标签之前对阵列进行随机播放。

选中此Randomly shuffle a Swift array