找不到类型 'UIAlertController' 的初始值设定项错误
Cannot find an initializer for type 'UIAlertController' ERROR
我对此有点陌生,请帮忙!
这是完整的错误:
Cannot find an initializer for type 'UIAlertController' that accepts an argument list of type '(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/")!)
}
感谢您的帮助
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)
我对此有点陌生,请帮忙!
这是完整的错误:
Cannot find an initializer for type 'UIAlertController' that accepts an argument list of type '(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/")!)
}
感谢您的帮助
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)