2015-06-10 58 views
1

我已經爲我的應用程序設置了一個alertcontroller,它的工作方式是如果部分得分高於10,你會得到一個UI警報。多個UIAlertControllers在Swift中一個接一個地顯示

我現在的問題是,如果我有2個或3個部分超過10,我只得到了第一個UIalert顯示,ID喜歡看所有的人一前一後(如果這sutuation發生

這裏我的代碼:。

func SectionAlert() { 

    var message1 = NSLocalizedString("Section 1 score is now ", comment: ""); 
    message1 += "\(section1score)"; 
    message1 += NSLocalizedString(" please review before continuing", comment: "1"); 

    var message2 = NSLocalizedString("Section 2 score is now ", comment: ""); 
    message2 += "\(section2score)"; 
    message2 += NSLocalizedString(" please review before continuing", comment: "2"); 

    var message3 = NSLocalizedString("Section 3 score is now ", comment: ""); 
    message3 += "\(section3score)"; 
    message3 += NSLocalizedString(" please review before continuing", comment: "3"); 

    if (section1score >= 10){ 
     let alertController: UIAlertController = UIAlertController(title: NSLocalizedString("Section 1 score is over 10", comment: ""), 
      message: " \(message1)", 
      preferredStyle: .Alert) 

     let OKAction = UIAlertAction(title: "OK", style: .Default) { 
      action -> Void in } 

     alertController.addAction(OKAction) 
     self.presentViewController(alertController, animated: true, completion: nil) 

    } else if (section2score >= 10){ 
     let alertController: UIAlertController = UIAlertController(title: NSLocalizedString("Section 2 Score is over 10", comment: ""), 
      message: "\(message2)", 
      preferredStyle: .Alert) 

     let OKAction = UIAlertAction(title: "OK", style: .Default) { 
      action -> Void in } 

     alertController.addAction(OKAction) 
     self.presentViewController(alertController, animated: true, completion: nil) 

    } else if (section3score >= 10){ 
     let alertController: UIAlertController = UIAlertController(title: NSLocalizedString("Section 3 Score is over 10", comment: ""), 
      message: "\(message3)", 
      preferredStyle: .Alert) 

     let OKAction = UIAlertAction(title: "OK", style: .Default) { 
      action -> Void in } 

     alertController.addAction(OKAction) 
     self.presentViewController(alertController, animated: true, completion: nil) 
    } 
} 

任何想法?

感謝

回答

0

好的,我已經弄明白了,我所做的是獲取要在按下OK時運行的代碼該視圖,以便它檢查其他部分,然後在需要時彈出另一個部分。

action -> Void in 

非常感謝後,推杆它

0

的主要問題是,你正在使用else if的部分除非先前的條件評估爲false,否則將不會測試兩個和三個條件。

所以,你想改變這一點:

if (section1score >= 10){ 
    // … 
} else if (section2score >= 10){ 
    // … 
} else if (section3score >= 10){ 
    // … 
} 

看起來更像這樣:

if (section1score >= 10){ 
    // … 
} 

if (section2score >= 10){ 
    // … 
} 

if (section3score >= 10){ 
    // … 
} 

這就是說,你將無法在同一時間呈現三個視圖控制器。您可能需要更新您的代碼以將這些消息合併爲一個警報。 (這會比看到三個模式警報同時出現更好的用戶體驗。)

+0

嗯,我想獲得連續3個警告,如果需要的是有,就是要與如果只發生?因爲我試過這樣並沒有得到好的結果 – Jp4Real

+0

如果你願意,你可以這樣做,但你需要適當的時間視圖控制器演示。你不能一次呈現全部。 –

+0

(有關更多討論,請參閱[連續UIAlertControllers](http://stackoverflow.com/q/25932589/1445366);這不像UIAlertView那樣支持開箱即用。) –

相關問題