Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用UIAlertController阻塞循环_Ios_Objective C_Swift_Uialertcontroller - Fatal编程技术网

Ios 使用UIAlertController阻塞循环

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循环吗

我有一个循环,在它里面,它用一个对话框询问用户。我想在用户按OK或Cancel后暂停循环

可能吗

因为循环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);
    }
}

不要创建循环,而是实现一个逻辑方法,在用户做出决定后调用自己

更改方法实现,如:

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