2016-02-28 30 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