【问题标题】:Multiple UIAlertControllers to show one after the other in Swift多个 UIAlertController 在 Swift 中一个接一个地显示
【发布时间】:2015-06-10 13:30:23
【问题描述】:

我已经为我的应用设置了一个警报控制器,它的工作方式是如果部分分数高于 10,你会收到一个 UI 警报。

现在我的问题是,如果我有超过 10 个的 2 或 3 个部分,我只会显示第一个 UIalert,我希望一个接一个地看到所有这些(如果发生这种情况

这是我的代码:

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

有什么想法吗??

谢谢!

【问题讨论】:

    标签: ios swift uialertcontroller


    【解决方案1】:

    主要问题是您使用的是else if。除非前面的条件评估为 false,否则不会测试第二节和第三节条件。

    所以你想改变这个:

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

    看起来更像这样:

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

    也就是说,您不能同时呈现三个视图控制器。您可能需要更新代码以将消息合并到一个警报中。 (这将比同时看到三个模式警报更好的用户体验。)

    【讨论】:

    • 好吧,如果需要的话,我想获得 3 个连续的警报,如果只有这种情况会发生吗?因为我试过这样并没有得到好的结果
    • 如果您愿意,您可以这样做,但您需要适当地安排视图控制器演示的时间。您不能一次全部展示它们。
    • (有关更多讨论,请参阅 Consecutive UIAlertControllers;不像 UIAlertView 那样开箱即用。)
    • 我应该把它改成 UIalertview
    • 否,UIAlertView 在 iOS 8 和 9 中已弃用。
    【解决方案2】:

    好的,我已经想通了,我所做的是在我在视图上按 OK 时运行代码,以便它检查其他部分,然后在需要时弹出另一个部分。

    我已经把它放在后面了

    action -> Void in
    

    非常感谢

    【讨论】:

      猜你喜欢
      • 2016-01-31
      • 1970-01-01
      • 2018-07-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多