Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
[ad_1]
Lengthy story quick, I used to be fairly bored with tagging views with silly quantity values, so I regarded for a greater different resolution to repair my downside. Because it turned out, there’s a property referred to as accessibilityIdentifier
that may do the job.
extension UIView {
var id: String? {
get {
return self.accessibilityIdentifier
}
set {
self.accessibilityIdentifier = newValue
}
}
func view(withId id: String) -> UIView? {
if self.id == id {
return self
}
for view in self.subviews {
if let view = view.view(withId: id) {
return view
}
}
return nil
}
}
I made a easy extension across the UIView class, so now I can use a correct string worth to uniquely establish any view object within the view hierarchy. It is fairly a pleasant resolution, now I can identify my views in a very nice means. As a free of charge storing the identify beneath the accessibilityIdentifier
will profit your UI assessments. 😉
The principle thought is to have an Int
based mostly enum for each view identifier, so mainly you need to use the tag
property to retailer the enum’s rawValue
. It is nonetheless not so good because the one above, but it surely’s far more secure than counting on pure integers. 😬
enum ViewIdentifier: Int {
case submitButton
}
extension UIView {
var identifier: ViewIdentifier? {
set {
if let worth = newValue {
self.tag = worth.rawValue
}
}
get {
return ViewIdentifier(rawValue: self.tag)
}
}
func view(withId id: ViewIdentifier) -> UIView? {
return self.viewWithTag(id.rawValue)
}
}
Truthfully I simply got here up with the second method proper after I copy & pasted the primary snippet to this text, however what the heck, perhaps another person will prefer it. 😂
In case you have a greater resolution for this downside, be at liberty to share it by way of twitter. Oh, by the way in which, do not forget to subscribe to my month-to-month e-newsletter under.
[ad_2]