Created
November 3, 2010 03:53
-
-
Save rjungemann/660777 to your computer and use it in GitHub Desktop.
Load a view from a nib/xib in Objective C
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
class ViewNib | |
def self.load(name) | |
nib = NSNib.alloc.initWithNibNamed(name, bundle: nil) | |
objects = Pointer.new_with_type('@') | |
view = nil | |
nib.instantiateNibWithOwner(NSApp, topLevelObjects: objects) | |
objects[0].each { |object| view = object if object.is_a? NSView } | |
view.removeFromSuperview if view | |
view | |
end | |
end |
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
class ViewNib | |
def self.load(name) | |
nib = NSNib.alloc.initWithNibNamed(name, bundle: nil) | |
objects = Pointer.new_with_type('@') | |
nib.instantiateNibWithOwner(NSApp, topLevelObjects: objects) | |
(class << self; self; end).class_eval { | |
def views | |
vs = [] | |
objects[0].each { |object| | |
view = object if object.is_a? NSView | |
view.removeFromSuperview if view | |
vs << view | |
} | |
vs | |
end | |
} | |
objects | |
end | |
end | |
main_view = ViewNib.load("views.xib").views.first |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment