找不到类型' UIAlertController'的初始值设定项。 ERROR

时间:2015-05-19 02:30:40

标签: ios swift uialertcontroller

我对此有点新意,请帮助!

下面是完整的错误:

  

找不到类型' UIAlertController'的初始值设定项。接受类型'的参数列表(title:String,message:(Int),preferredStyle:UIAlertControllerStyle)'

这是我的代码中发生错误的部分:

import UIKit

class ViewController: UIViewController, UITextFieldDelegate, UITextViewDelegate{

@IBOutlet weak var CalcButton: UIButton!
@IBOutlet weak var DaysLabel: UITextView!
@IBOutlet weak var RateLabel: UITextView!
@IBOutlet weak var CowsLabel: UITextView!
@IBOutlet weak var PoundsLabel: UITextView!

@IBOutlet weak var PoundsTextfield: UITextField!
@IBOutlet weak var CowsTextfield: UITextField!
@IBOutlet weak var DaysTextfield: UITextField!
@IBOutlet weak var RateTextfield: UITextField!
@IBOutlet weak var Label: UILabel!

@IBAction func buttonCalc(sender: AnyObject) {
    var Days: Int = DaysTextfield.text.toInt()!
    var Pounds: Int = PoundsTextfield.text.toInt()!
    var Cows: Int = CowsTextfield.text.toInt()!
    var Rate: Int = RateTextfield.text.toInt()!

    if Days > 0 && Pounds > 0 && Cows > 0 && Rate > 0 {
        Label.text = String(stringInterpolationSegment: Days)
        var sDays = String(Days)
        var sCows = String(Cows)
        let alertController = UIAlertController(title: "Based on the values you entered, this is what the bonus is for \(sCows) cows and \(sDays):", message:
            (Rate/100 * Pounds) * (Cows * Days), preferredStyle: UIAlertControllerStyle.Alert)
        alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default,handler: nil))

        self.presentViewController(alertController, animated: true, completion: nil)
    }  
}

@IBAction func NDHIALink(sender: AnyObject) {
   UIApplication.sharedApplication().openURL(NSURL(string: "http://www.dhia.org/")!)
}

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

UIAlertController(title: "Based on the values you entered, this is what the bonus is for \(sCows) cows and \(sDays):", message:
            (Rate/100 * Pounds) * (Cows * Days), preferredStyle: UIAlertControllerStyle.Alert)

只需将(Rate/100 * Pounds) * (Cows * Days转换为String

即可
UIAlertController(title: "Based on the values you entered, this is what the bonus is for \(sCows) cows and \(sDays):", message:
                String((Rate/100 * Pounds) * (Cows * Days)), preferredStyle: UIAlertControllerStyle.Alert)