2017-01-01 62 views
0

我試圖在按下按鈕時彈出警報。我希望這個藝術的題目是「你想挖掘材料」,並給出兩個選擇;是和不是。如果選擇是,那麼我希望這個被實施UIAlertView出錯

Ironcnt.setvalue(forkey: "Iron")-3 

如果沒有選擇,那麼警報視圖將消失。這是我寫的代碼。當我在模擬器中測試它時,它會崩潰。我還附上了調試器所說的截圖。

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 
    } 
} 

enter image description here

回答

3

問題

你叫兩次present(_:animated:completion:)將每個動作後呈現報警控制器。

解決方案

只是把它添加行爲之後的一個時間,並添加您的代碼應選擇在它的處理程序是動作後執行。

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) 
1

你試圖提出警告兩次。 設置第一個動作後,刪除第一個self.present(alert, animated:true, completion:nil)