Last active
May 27, 2016 19:53
-
-
Save aral/9623200da8e44c4966b0 to your computer and use it in GitHub Desktop.
Tag support in Interface Builder for custom NSViews
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NSView+CustomTaggable.swift | |
// | |
// Allows you to set tags in Interface Builder for custom NSViews. | |
// | |
// Usage: | |
// | |
// Your custom view must implement the CustomTaggable protocol. Then, in your custom | |
// NSView, declare the customTag property like this: | |
// | |
// @IBDesignable | |
// class MyView: NSView, CustomTaggable | |
// { | |
// @IBInspectable var customTag:Int = -1 | |
// //… | |
// } | |
// | |
// Ideally, Apple should allow custom NSViews to have regular tags set for them in IB. | |
// | |
// Please dupe this radar if you agree: rdar://21888110 | |
// (View text of radar on Open Radar at http://openradar.appspot.com/21888110) | |
// | |
// Created by Aral Balkan on 18/07/2015. | |
// Copyright (c) 2015 Ind.ie. Released under the MIT License. | |
// | |
import Cocoa | |
extension NSView | |
{ | |
func viewWithCustomTag(customTag:Int) -> NSView? | |
{ | |
// | |
// Iterates over subviews and returns the view with the specified custom tag. | |
// The custom view must implement the CustomTaggable protocol. | |
// | |
for view in subviews | |
{ | |
if let view = view as? NSView, customTaggableView = view as? CustomTaggable | |
{ | |
if customTaggableView.customTag == customTag | |
{ | |
return view | |
} | |
} | |
} | |
return nil | |
} | |
} | |
protocol CustomTaggable | |
{ | |
var customTag:Int {get set} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment