如何将新的字符串值添加到UIAlertController的标题中

时间:2016-02-16 00:40:18

标签: ios swift2 uialertcontroller

我的UIAlertController存在问题。我用UIAlertController创建了一个名为presentWinningAlert的函数,该函数具有标题"获胜者是"。然后我创建了一个名为checkForWinner的函数,我想将标签中的文本添加到标题UIAlertController中,使其成为" Winner是(符合条件的任何标签)"但我在checkForWinner函数中使用的方式不起作用的另一种方法

import UIKit

class ViewController: UIViewController {

    @IBOutlet var drag: UIPanGestureRecognizer!
    @IBOutlet weak var currentLabel: UILabel!
    @IBOutlet weak var label1: UILabel!
    @IBOutlet weak var label2: UILabel!
    @IBOutlet weak var label3: UILabel!
    @IBOutlet weak var label4: UILabel!
    @IBOutlet weak var label5: UILabel!
    @IBOutlet weak var label6: UILabel!
    @IBOutlet weak var label7: UILabel!
    @IBOutlet weak var label8: UILabel!
    @IBOutlet weak var label9: UILabel!

    let letter = "OX"
    var currentLetter = Character!()
    var decide = 0
    var labelArray = [UILabel]()
    override func viewDidLoad() {
        super.viewDidLoad()
        getCurrentLetter()
        labelArray = [label1,label2,label3,label4,label5,label6,label7,label8,label9]
    }

    @IBAction func tapped(sender: UITapGestureRecognizer) {
        let selectedPointed = sender.locationInView(self.view)
        for item in labelArray {
            if CGRectContainsPoint(item.frame, selectedPointed) {
                item.text = String(currentLetter)
                decide++
            }
            if decide == 2 {
                reset()
            }
        }
        checkForWinner()
        getCurrentLetter()

    }

    func getCurrentLetter()
    {
        let myRange = Range<String.Index>(start: letter.startIndex.advancedBy(decide), end: letter.startIndex.advancedBy(decide+1))
        currentLabel.text = letter.substringWithRange(myRange)
        currentLetter = Character(letter.substringWithRange(myRange))
    }
    func reset() {
        decide = 0
    }

    func checkForWinner()
    {

        if label1.text == label2.text && label2.text == label3.text && label3.text != ""
        {
            presentWinningAlert("\(label3.text!)")
        }
        else if label4.text == label5.text && label5.text == label6.text && label6.text != ""
        {
            presentWinningAlert("\(label6.text!)")
        }
        else if label7.text == label8.text && label8.text == label9.text && label9.text != ""
        {
            presentWinningAlert("\(label9.text!)")
        }
        else if label1.text == label4.text && label4.text == label7.text && label7.text != ""
        {
            presentWinningAlert("\(label7.text!)")
        }
        else if label2.text == label5.text && label5.text == label8.text && label8.text != ""
        {
            presentWinningAlert("\(label8.text!)")
        }
        else if label3.text == label6.text && label6.text == label9.text && label9.text != ""
        {
            presentWinningAlert("\(label9.text!)")
        }
        else if label1.text == label5.text && label5.text == label9.text && label9.text != ""
        {
            presentWinningAlert("\(label9.text!)")
        }
        else if label3.text == label5.text && label5.text == label7.text && label7.text != ""
        {
            presentWinningAlert("\(label7.text!)")
        }
        if decide == 9
        {
            presentWinningAlert("No One")
        }
    }

    func presentWinningAlert(alert: String) {
        let presentAlert = UIAlertController(title: "The Winner is", message: nil, preferredStyle: .Alert)
        let reset = UIAlertAction(title: "Reset", style: UIAlertActionStyle.Default, handler: clearLabels)
        presentAlert.addAction(reset)
        presentViewController(presentAlert, animated: true, completion: nil)
    }
}

1 个答案:

答案 0 :(得分:0)

这是非常基本的东西。按如下方式更改您的功能:

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        test_case * t = [[test_case alloc] init];
        [t run];
        return NSApplicationMain(argc, argv);
    }
}