I have a program that I'm working on that takes an array of strings and iterates through this list and creates a binary tree depending on what the user selects from a head to head matchup.
The problem is that I'm wanting to have the program stop and wait for the user to select an option within the alert controller before continuing through the nested recursive functions. It is not doing this. It actually runs through the code without stopping and displays the alert controller at the end.
I have one recursive function to iterate through the array and within each option I have a recursive function to place where in the binary tree i want the node to be created with the string name.
The code is pasted below.
func iterate(value: Int){
if unsortedArray.count > value { addNode(name: unsortedArray[value]) } } func addNode(name: String){
if self.root == nil { self.root = Node(nameValue: name) } else { self.addNode(name: name, node: root!) } //raises the value of iterator int by one and calls the iterate function again to access the next string in the array iterator+=1 self.iterate(value: iterator) } private func addNode(name: String, node: Node){ showPopUp(name: name, node: node) } func showPopUp(name: String, node: Node){ //calls an alert dialog using the names of the two string items as the names of the button.
let alert = UIAlertController(title: "Head to Head Comparison", message: "Select which item you prefer", preferredStyle: .alert) let nameAction = UIAlertAction(title: name, style: .default) { (UIAlertAction) in self.output.append("\(name) was selected over \(node.nodeName) \r\n") if node.rightChild == nil { node.rightChild = Node(nameValue: name) } else { self.addNode(name: name, node: node.rightChild!) } } let nodeAction = UIAlertAction(title: node.nodeName, style: .default) { (UIAlertAction) in self.output.append("\(node.nodeName) was selected over \(name) \r\n") if node.leftChild == nil { node.leftChild = Node(nameValue: name) } else { self.addNode(name: name, node: node.leftChild!) } } alert.addAction(nameAction) alert.addAction(nodeAction) present(alert, animated: true, completion: nil) } https://stackoverflow.com/questions/66773781/swift-stopping-a-nested-recursive-function-with-an-alert-controller-for-user-in March 24, 2021 at 10:07AM
没有评论:
发表评论