Skip to content

Instantly share code, notes, and snippets.

@rjungemann
Created November 3, 2010 03:53
Show Gist options
  • Save rjungemann/660777 to your computer and use it in GitHub Desktop.
Save rjungemann/660777 to your computer and use it in GitHub Desktop.
Load a view from a nib/xib in Objective C
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
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