使用 UIAlertController 阻塞循环
Blocking a Loop Using UIAlertController
我们有没有办法阻止 for 循环?
我在其中有一个循环,它通过对话框询问用户。我希望它在用户按下确定或取消后暂停循环。
可能吗?
因为循环 alertController 看起来真的很难看,尤其是当你有很多循环时。
for request in friendRequests
{
let alertController = UIAlertController(title: "Friend Request", message: "Would you like to accept this friend request?", preferredStyle: UIAlertControllerStyle.Alert);
let cancelAction: UIAlertAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel, handler: nil);
let confirmAction: UIAlertAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil);
alertController.addAction(cancelAction);
alertController.addAction(confirmAction);
//How do we block or wait until user press a button??
self.navigationController.presentViewController(alertController);
}
有什么想法吗?谢谢
不是创建循环,而是实现一个在用户决定后调用自身的逻辑方法。
更改方法实现,例如:
func showAlert(var requestIndex : Int)
{
if (requestIndex < friendRequests.count)
{
var request = friendRequests[requestIndex]
requestIndex++
let alertController = UIAlertController(title: "Friend Request", message: "Would you like to accept this friend request?", preferredStyle: UIAlertControllerStyle.Alert);
let cancelAction: UIAlertAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel, handler:{ action in
showAlert(requestIndex)
});
let confirmAction: UIAlertAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { action in
showAlert(requestIndex)
});
alertController.addAction(cancelAction);
alertController.addAction(confirmAction);
//How do we block or wait until user press a button??
self.navigationController.presentViewController(alertController);
}
}
最初调用方法如下:
showAlert(0);
使用递归函数而不是循环
例子
var name:[String] = ["abcd","efgh","ijkl","mnop","qrst","uvwx"]
self.friendReuqest(name, index: 0)
func friendReuqest(name:[String],index:Int)
{
if index < name.count
{
let alertController = UIAlertController(title: name[index], message: "Would you like to accept this friend request?", preferredStyle: UIAlertControllerStyle.Alert)
let cancelAction: UIAlertAction = UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Cancel){ (action: UIAlertAction!) -> Void in
// do stuff here when press cancel
}
let confirmAction: UIAlertAction = UIAlertAction(title: "Add", style: UIAlertActionStyle.Default) { (action: UIAlertAction!) -> Void in
self.friendReuqest(name, index: (index + 1))
}
alertController.addAction(cancelAction)
alertController.addAction(confirmAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
}
我们有没有办法阻止 for 循环?
我在其中有一个循环,它通过对话框询问用户。我希望它在用户按下确定或取消后暂停循环。
可能吗?
因为循环 alertController 看起来真的很难看,尤其是当你有很多循环时。
for request in friendRequests
{
let alertController = UIAlertController(title: "Friend Request", message: "Would you like to accept this friend request?", preferredStyle: UIAlertControllerStyle.Alert);
let cancelAction: UIAlertAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel, handler: nil);
let confirmAction: UIAlertAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil);
alertController.addAction(cancelAction);
alertController.addAction(confirmAction);
//How do we block or wait until user press a button??
self.navigationController.presentViewController(alertController);
}
有什么想法吗?谢谢
不是创建循环,而是实现一个在用户决定后调用自身的逻辑方法。
更改方法实现,例如:
func showAlert(var requestIndex : Int)
{
if (requestIndex < friendRequests.count)
{
var request = friendRequests[requestIndex]
requestIndex++
let alertController = UIAlertController(title: "Friend Request", message: "Would you like to accept this friend request?", preferredStyle: UIAlertControllerStyle.Alert);
let cancelAction: UIAlertAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel, handler:{ action in
showAlert(requestIndex)
});
let confirmAction: UIAlertAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { action in
showAlert(requestIndex)
});
alertController.addAction(cancelAction);
alertController.addAction(confirmAction);
//How do we block or wait until user press a button??
self.navigationController.presentViewController(alertController);
}
}
最初调用方法如下:
showAlert(0);
使用递归函数而不是循环
例子
var name:[String] = ["abcd","efgh","ijkl","mnop","qrst","uvwx"]
self.friendReuqest(name, index: 0)
func friendReuqest(name:[String],index:Int)
{
if index < name.count
{
let alertController = UIAlertController(title: name[index], message: "Would you like to accept this friend request?", preferredStyle: UIAlertControllerStyle.Alert)
let cancelAction: UIAlertAction = UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Cancel){ (action: UIAlertAction!) -> Void in
// do stuff here when press cancel
}
let confirmAction: UIAlertAction = UIAlertAction(title: "Add", style: UIAlertActionStyle.Default) { (action: UIAlertAction!) -> Void in
self.friendReuqest(name, index: (index + 1))
}
alertController.addAction(cancelAction)
alertController.addAction(confirmAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
}