Ios 使用UIAlertController阻塞循环
我们有办法阻止for循环吗 我有一个循环,在它里面,它用一个对话框询问用户。我想在用户按OK或Cancel后暂停循环 可能吗 因为循环alertController看起来非常难看,特别是当你有很多循环时Ios 使用UIAlertController阻塞循环,ios,objective-c,swift,uialertcontroller,Ios,Objective C,Swift,Uialertcontroller,我们有办法阻止for循环吗 我有一个循环,在它里面,它用一个对话框询问用户。我想在用户按OK或Cancel后暂停循环 可能吗 因为循环alertController看起来非常难看,特别是当你有很多循环时 for request in friendRequests { let alertController = UIAlertController(title: "Friend Request", message: "Would you like to accept this friend
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);
}
}
不要创建循环,而是实现一个逻辑方法,在用户做出决定后调用自己 更改方法实现,如:
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);
}
}
使用递归函数而不是循环 范例
使用递归函数而不是循环 范例
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)
}
}