I have already implemented an UITextField that segues to two different ViewControllers based on information inserted into the UITextField if the information entered matches my array {bdArray} then when my button (bedButton) is pressed the view controller segues to viewControllerA if it does not match the correct information in my bdArray it segues to viewControllerB. My problem is i want to allow Segue to ViewControllerA if the first word or one off the words in the UITextField matches my bdArray, then segue to viewControllerA will happen. How will i implement this method?
@IBAction func bYo(sender: AnyObject) {
let pText = pTextField.text?.uppercaseString ?? ""
let pDestination = activeP.map { $0.uppercaseString }.contains(pText) ? "mM" : "nDy"
performSegueWithIdentifier(pDestination, sender: sender) }
this is my code i have already for making the text in the UItextField not case sensitive, which is needed. Any way to add any code to do the part search?
First you need to split the textField's text into an array of String, then check if bdArray contains one or more elements in the String array.
let numOfMatches = textField.text?.characters.split { $0 == " " }.filter { bdArray.contains(String($0)) }.count
if numOfMatches > 0 {
// segue to view controller A
} else {
// segue to view controller B
}
According to the code you posted, you might want to rewrite it like so:
@IBAction func bYo(sender: AnyObject) {
let pText = pTextField.text?.uppercaseString ?? ""
activeP = activeP.map { $0.uppercaseString }
let pTextArray = pText.characters.split { $0 == " " }
var numOfMatches = 0
var pDestination = ""
if pTextArray.count == 1 {
numOfMatches = activeP.filter { pText.containsString($0) }.count
} else {
numOfMatches = pTextArray.filter { activeP.contains(String($0)) }.count
}
numOfMatches > 0 ? (pDestination = "mM") : (pDestination = "nDy")
performSegueWithIdentifier(pDestination, sender: sender)
}