我正在创建一个返回布尔值的函数。但是 Swift 向我展示了错误:
预期返回“Bool”的函数中缺少返回值
我的函数不直接返回“布尔”,它有一些条件,比如如果用户点击“确定”按钮,那么它应该返回真,如果“取消”则返回假。有人可以告诉我如何解决这个问题吗?请参阅以下代码以供参考:
func showConfirmationAlert(title: String!, message: String!) -> Bool {
dispatch_async(dispatch_get_main_queue(), {
let alertController=UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
let okButton=UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default) { (okSelected) -> Void in
return true
}
let cancelButton=UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (cancelSelected) -> Void in
return false
}
alertController.addAction(okButton)
alertController.addAction(cancelButton)
if self.presentedViewController == nil {
self.presentViewController(alertController, animated: true, completion: nil)
}
})
}
Run Code Online (Sandbox Code Playgroud) 我已经写代码,出现报警时,在我的UITextFields的一个输入小于1050中输入满足这一点,但我后按"确定"它会立即重新出现时出现成功.
下面是viewDidLoad函数中的代码:
override func viewDidLoad(){
super.viewDidLoad()
alert = UIAlertController(title: "Error", message: "Please enter an exit width value greater than 1050", preferredStyle: UIAlertControllerStyle.Alert)
let okay = UIAlertAction(title: "OK", style: UIAlertActionStyle.Destructive, handler: valueCalc)
alert.addAction(okay)
}
Run Code Online (Sandbox Code Playgroud)
然后我有我的valueCalc功能(当点击按钮时调用):
@IBAction func valueCalc(sender: AnyObject){
if(Int(mmText.text!)! < 1050){ //mmText is an UITextField
self.presentViewController(alert, animated: true, completion: nil)
}
}
Run Code Online (Sandbox Code Playgroud) 我的iOS应用程序需要一组具有类似功能的自定义警报.我可以将它们打包在一个类中,所以我不必在每个UIViewController中复制粘贴吗?
func displayAlert(msg:String, handler: (UIAlertAction) -> Void?{...}
func successMsg (msg: String){...}
func failMsg(msg: String){...}
Run Code Online (Sandbox Code Playgroud)
我试图将它们打包到UIViewController的子类(AlertViewController)中,但之后出现了运行时错误:"尝试在<... UIAlertController ...>上显示<... UIAlertController ...>,其视图不在窗口层层叠叠!"
myViewController中的代码:
@IBAction func leave() {
let date = getDate()
let datePresent = dateForPresent()
if stID == nil || name == nil {
return
} else {
alert.displayAlert("\(datePresent)"){action in
self.request.getResult(self.request.leavePara(self.stID!, date: date, name: self.name!) ){ (result) in
dispatch_async(dispatch_get_main_queue(), {
let flag = result["result","out_Flag"].int
print(result)
let msg = result["result","out_nszRtn"].string!
if flag == 0 {
self.alert.successMsg(msg){ action in
self.performSegueWithIdentifier("personUnwind", sender: self)
}
}else { …Run Code Online (Sandbox Code Playgroud) 我有一个场景,可能需要重命名某些内容,我想用它UIAlertController来收集新名称。我希望它能够同时进行多次更改(虽然不太可能,但我想支持它)。
问题是我只需要在当前的结果出现时循环才能继续UIAlertController,事实上,循环会立即继续,当然一次不能出现多个。我一整天都被困在这个问题上,需要帮助。
我需要某种使用完成处理程序为下一个循环迭代提供绿灯的方法。
这是问题的简化版本,changeNames 函数位于UIViewController.
var names = ["Bob","Kate"]
func changeNames(){
for name in names {
let alert = UIAlertController(title: "Rename \(name)",
message: "What would you like to call \(name)?",
preferredStyle: .alert)
alert.addTextField(configurationHandler: { (textField) in
textField.text = name
})
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { [weak alert] (_) in
print("User changed \(name) to \(alert!.textFields![0].text)")
//Don't continue the loop until now
}))
self.present(alert, animated: true)
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢
我面临如下问题,它发生在iOS 10.x中
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
alertController.popoverPresentationController?.sourceView = label
alertController.popoverPresentationController?.sourceRect = label.bounds
let action1 = UIAlertAction(title:"test", style: .default) { [weak self] _ in
}
let action2 = UIAlertAction(title:"test" style: .default) { [weak self] _ in
}
alertController.addAction(action1)
alertController.addAction(action2)
present(alertController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
当我打开这个带有标签点击的警报控制器时,我看到我的alertcontroller作为附加屏幕.它的按钮不可见.
2018-02-10 23:13:16.814325 Project[27392:1655456] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each …Run Code Online (Sandbox Code Playgroud)