【问题标题】:Cannot find an initializer for type 'UIAlertController' ERROR找不到类型“UIAlertController”错误的初始化程序
【发布时间】:2015-05-19 02:30:39
【问题描述】:

我对此有点陌生,请帮忙!

这是完整的错误:

找不到接受类型为“(title: String, message: (Int), preferredStyle: UIAlertControllerStyle)”的参数列表的“UIAlertController”类型的初始化程序

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

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/")!)
}

感谢您的帮助

【问题讨论】:

    标签: ios swift uialertcontroller


    【解决方案1】:
    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-19
      • 2017-11-16
      • 1970-01-01
      • 2011-05-16
      相关资源
      最近更新 更多