首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIAlertView错误

UIAlertView错误
EN

Stack Overflow用户
提问于 2017-01-01 06:45:51
回答 2查看 266关注 0票数 0

一旦按下按钮,我正试着弹出一个警报。我希望这幅画的标题是“你愿意挖掘材料吗”,并给出两种选择:是和否。如果选择是,那么我希望实现这一点。

代码语言:javascript
复制
Ironcnt.setvalue(forkey: "Iron")-3

如果选择no,则警报视图将消失。这是我写的代码。当我在模拟器里测试这个时,它崩溃了。我还附上了调试器所说的截图。

代码语言:javascript
复制
import UIKit

let Ironcnt = UserDefaults.standard
let Goldcnt = UserDefaults.standard
let Fiveth = UserDefaults.standard

let shipcnt = UserDefaults.standard
let empirecnt = UserDefaults.standard

var fiveth = Int()
var iron = Int()
var gold = Int()
var ships = Int()
var empires = Int()

class one: UIViewController {
    @IBOutlet weak var fiveth1: UILabel!
    @IBOutlet weak var iron1: UILabel!
    @IBOutlet weak var gold1: UILabel!
    @IBOutlet weak var ships1: UILabel!
    @IBOutlet weak var empire1: UILabel!
    @IBOutlet weak var planet: UIImageView!

@IBOutlet weak var webViewBG: UIWebView!

@IBAction func emp(_ sender: Any) {
    let alert = UIAlertController(title:"title", message:"this is an alert", preferredStyle:UIAlertControllerStyle.alert); alert.addAction(UIAlertAction(title:"action1",style: UIAlertActionStyle.default, handler:nil)); self.present(alert, animated:true, completion:nil)
}

@IBAction func mat(_ sender: Any) {
    let alert = UIAlertController(title:"Would you like to mine for materials?", message:"This will cost you two Fiveth", preferredStyle:UIAlertControllerStyle.alert);

    alert.addAction(UIAlertAction(title:"No", style: UIAlertActionStyle.default, handler:nil)); self.present(alert, animated:true, completion:nil)

    alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.default , handler: nil));
        self.present(alert, animated:true, completion:nil)
}

override func viewDidLoad() {
    super.viewDidLoad()

    let htmlPath = Bundle.main.path(forResource: "WebViewContent", ofType: "html")
    let htmlURL = URL(fileURLWithPath: htmlPath!)
    let html = try? Data(contentsOf: htmlURL)

    self.webViewBG.load(html!, mimeType: "text/html", textEncodingName: "UTF-8", baseURL: htmlURL.deletingLastPathComponent())

    if (Fiveth.value(forKey: "Fiveth") != nil){
        fiveth = Fiveth.value(forKey: "Fiveth") as! NSInteger!
        fiveth1.text = NSString(format: "Fiveth: %i", fiveth) as String
    }
    if (Ironcnt.value(forKey: "Ironcnt") != nil){
        iron = Ironcnt.value(forKey: "Ironcnt") as! NSInteger!
        iron1.text = NSString(format: "Iron: %i", iron) as String
    }
    if (Goldcnt.value(forKey: "Goldcnt") != nil){
        gold = Goldcnt.value(forKey: "Goldcnt") as! NSInteger!
        gold1.text = NSString(format: "Gold: %i", gold) as String
    }
}

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-01-01 07:11:32

问题

在添加每个操作后,您将调用两次present(_:animated:completion:)来显示警报控制器。

溶液

只需在添加操作后调用它一次,然后添加在其处理程序中选择yes操作后应该执行的代码。

代码语言:javascript
复制
let alert = UIAlertController(title: "Would you like to mine for materials?", message: "This will cost you two Fiveth", preferredStyle: .alert)
let noAction = UIAlertAction(title: "No", style: .default, handler: nil)
let yesAction = UIAlertAction(title: "Yes", style: .default) { _ in
  // here you can add your code which should be executed after choosing the yes action
  Ironcnt.setvalue(forkey: "Iron")-3
}

alert.addAction(noAction)
alert.addAction(yesAction)
present(alert, animated: true, completion: nil)
票数 3
EN

Stack Overflow用户

发布于 2017-01-01 07:08:52

你想发出两次警告。在设置第一个操作后删除第一个self.present(alert, animated:true, completion:nil)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41413847

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档