2016-02-28 62 views
0

我正在寻找一种方法来找到一种更有效的方法来将多个UITextFields(在这种情况下,donorView1,donorView2等)我的报价变化后,只需点击一个UIButton。点击Randomize按钮会产生一个引号数组,但是,使用这个引号数组,我只能得到每个字段一个引号,但是我需要在每个方框中引用不同的引号(即donorView1会有字母DQ,A,DP,DR而donorView2可能有A,C,B,DP)。我对这段代码很陌生 - 因为这是我第一次编写一个iOS应用程序 - 所以如果可能的话,请尝试用简单的语言来解释!每个文本字段至少需要6个随机字母。如何连续变更多个UITextField中的多个文本值?

@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. 
} 
+0

你为什么不使用'IBOutletCollection'代替'IBOutlet'? –

回答

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

+0

问题在于我的方程太复杂了,无法容纳6 quoteArray1 [Int(arc4random_uniform(randomRange))]]。我不知道如何简化我的公式,使其能够用6构建,但感谢您的帮助!真的很感激它。 –

+0

你是什么意思太复杂?你的要求是什么?每个文本字段是否应显示6个引号中的每一个的随机序列?如果是这样,你最好的办法是随机洗牌数组,并使用数组元素来构建每个textfield.text字符串。这对性能要好得多,你也可以打破6个限制。我会添加一个链接到答案。 – jarryd