UIAlertView错误

时间:2017-01-12 04:37:12

标签: swift uialertview

我正在使用Swift制作StockApplication,我正在尝试在投资组合中制作警报,但我遇到了一些错误。我查了最新版本的Alertview但没有任何工作,所以我只是在寻求帮助。

static func showAlert(title: String, message: String, caller: UITableViewController) {
    if #available(iOS 8.0, *) {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
    } else {
        // Fallback on earlier versions
    }

    if #available(iOS 8.0, *) {
        let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
            UIAlertAction in
        }
    } else {
        // Fallback on earlier versions
    }

    if #available(iOS 8.0, *) {
        alertcontroller.addAction(okAction)
    } else {
        // Fallback on earlier versions
    }


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

我的错误我正在使用未解析的标识符alertcontroller。

2 个答案:

答案 0 :(得分:0)

您的代码全部拆分,您的alertController声明仅限于第一个if声明。

最好的方法是将所有代码正确分组:

斯威夫特2:

static func showAlert(title: String, message: String, caller: UITableViewController) {
    if #available(iOS 8.0, *) {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
        let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { action in
        // do something
        }
        alertcontroller.addAction(okAction)
        caller.presentViewController(alertController, animated: true, completion: nil)
    } else {
        // Fallback to UIAlertView on earlier versions
    }
}

当然,如果您不需要支持iOS 7,那么这一切都不需要。只是做:

static func showAlert(title: String, message: String, caller: UITableViewController) {
    let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
    let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { action in
        // do something
    }
    alertcontroller.addAction(okAction)
    caller.presentViewController(alertController, animated: true, completion: nil)

}

斯威夫特3:

static func showAlert(title: String, message: String, caller: UITableViewController) {
    if #available(iOS 8.0, *) {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
        let okAction = UIAlertAction(title: "OK", style: .default) { action in
            // do something
        }
        alertController.addAction(okAction)
        caller.present(alertController, animated: true, completion: nil)
    } else {
        // Fallback to UIAlertView on earlier versions
    }
}

答案 1 :(得分:-1)

看起来你在代码块中实例化alertController,你指定它只在iOS 8.0可用时执行。尝试这样的事情:

var alertController:UIAlertController?

if #available(iOS 8.0, *) {
    alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
} else {
    // Fallback on earlier versions
    // Do whatever else with the alertController
}

根据您所说的错误而导致的问题是,您尝试在名为UIAlertController的方法结尾处展示alertController,但是&#39 ; s告诉你没有任何名为alertController的属性,它告诉我你实例化它的代码行没有被调用,这意味着如果条件不满足。

我会这样做,它更新了更简单:

func showAlert(title: String, message: String, caller: UITableViewController) {

    let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
    let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default)
    alertController.addAction(okAction)

    caller.present(alertController, animated: true, completion: nil)
}