首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >停止任务,直到发生AlertController操作

停止任务,直到发生AlertController操作
EN

Stack Overflow用户
提问于 2016-06-27 19:42:34
回答 2查看 1.5K关注 0票数 2

我尝试从completion参数present执行一个任务,以便它只在UIAlertController关闭后才执行所需的函数。但是,在警报中采取行动之前调用了该函数。如何等到采取了操作后才执行函数?

代码语言:javascript
复制
let alert = UIAlertController(title: "Wild Card Played", message: "Choose your suit", preferredStyle : .alert);
for suit in suits {
    alert.addAction(UIAlertAction(title: suit, style: .default, handler: crazyEightPlayed))
                  }
self.present(alert, animated: true, completion: cpuTurn) //Upon completion call the cpuTurn() function
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-06-27 19:51:03

当前的问题是在向用户显示警报时调用cpuTurn,而不是当用户按下“not”时。正如您在这里的文件中看到的,self.present方法中的完成函数“在表示完成后执行。这个块没有返回值,也不接受任何参数。您可以为这个参数指定零。”该警报为用户出现,第一个UIViewController表示“我已经完成了警报的呈现”,然后运行cpuTurn函数。

您需要将代码放在UIAlertAction的处理程序中,您似乎已经有了。您应该将cpuTurn调用移动到crazyEightPlayed函数(或者至少从crazyEightPlayed调用cpuTurn )。

票数 2
EN

Stack Overflow用户

发布于 2016-06-27 20:12:03

您可以尝试禁用具有交互作用的视图的子视图。我会注意到这些子视图,然后才会激活这些子视图。

Swift 2:

代码语言:javascript
复制
var disabledSubviews = [UIView]()

let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action) in
    for subview in disabledSubviews {
        subview.userInteractionEnabled = true
    }
}))

self.presentViewController(alert, animated: true) { 
    for subview in self.view.subviews {
        if subview.userInteractionEnabled == true {
            disabledSubviews.append(subview)
            subview.userInteractionEnabled = false
        }
    }
}

Swift 3:

代码语言:javascript
复制
var disabledSubviews = [UIView]()

let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action) in
    for subview in disabledSubviews {
        subview.isUserInteractionEnabled = true
    }
}))
self.present(alert, animated: true) { 
    for subview in self.view.subviews {
        if subview.isUserInteractionEnabled == true {
            disabledSubviews.append(subview)
            subview.isUserInteractionEnabled = false
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38062001

复制
相关文章

相似问题

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