I am using 2 tableviews
both have textfiled
in tableviewcell
,
I am getting text of textfield in textFieldDidEndEdit
Forget about traversing the view hierarchy; it is error prone and the exact details may change in the future (and break your app horribly).
Instead, try this:
Get the text field's location on screen:
// Center on text field's own coordinate system
let position = textField.frame.center
// Center on Window's coordinate system
let positionOnWindow = textField.convert(position, to: nil)
Convert that point to the table view's coordinate system, and see if it corresponds to a row:
let positionInTable = tableView1.convert(positionOnWindow, from: nil)</strike>
if let indexPath = tableView1.indexPathForRow(at: positionInTable) {
// Text field is inside a row of table view 1
} else {
// Otherwise
}
Note: Code above is untested. May contain compiler errors.
Assuming both code snippets run on the same class (your table view delegate, data source and text field delegate) there should be no problem passing the value of positionOnWindow
around (it can all happen inside the text field delegate method).
As duly pointed by @rmaddy in the comments, the code above is too roundabout; you can directly get the position with:
let positionInTable = tableView1.convert(textField.frame.center, from: textField)
(no need the trip to the window's coordinate system and back)
Swift 3.x
Assuming your hierarchy of textFiled is:
TextField->Cell->TableView
Write this line of code in textFieldDidEndEditing
print(textField.superview?.superview)
if textField.superview?.superview == myTable1 {
}
else if textField.superview?.superview == myTable2 {
}
else {
}