Skip to content

Instantly share code, notes, and snippets.

@BradB132
Created July 3, 2015 16:13
Show Gist options
  • Select an option

  • Save BradB132/2720fec149a9e5b190fd to your computer and use it in GitHub Desktop.

Select an option

Save BradB132/2720fec149a9e5b190fd to your computer and use it in GitHub Desktop.
InterfaceBuilderTricks-1
private var stringTagHandle: UInt8 = 0
extension UIView {
//use Objective C Associated Object API to add this property to UIView
@IBInspectable public var stringTag:String? {
get {
if let object = objc_getAssociatedObject(self, &stringTagHandle) as? String {
return object
}
return nil
}
set {
objc_setAssociatedObject(self, &stringTagHandle, newValue, objc_AssociationPolicy(OBJC_ASSOCIATION_RETAIN_NONATOMIC))
}
}
//this should work in a similar way to viewWithTag:
public func viewWithStringTag(strTag:String) -> UIView? {
if stringTag == strTag {
return self
}
for view in subviews as! [UIView] {
if let matchingSubview = view.viewWithStringTag(strTag) {
return matchingSubview
}
}
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment