RubyCocoa MultipleNibTabView sample

November 7, 2007

The example from the Apple Developer Tools often show interesting techniques that can be successfully used in real projects. One of those is the MultipleNibTabView example (available under /Developer/Examples/InterfaceBuilder/MultipleNibTabView) , showing how you how to build user interfaces in a more modular way. This is the official project description (ReadMe.rtf):

This example application demonstrates how to use views housed in separate nibs. The main window has an NSTabView and an NSTextField. The window controller creates a SubViewController which loads in a nib and returns the nib’s view. The window controller inserts those views into each NSTabViewItem. The SubViewController sends a message to the Window Controller, requesting a pointer to the NSTextField. The widgets in each view applies gets that pointer by sending a message to the SubViewController and then applies changes to the text and color of the NSTextField.

Here it goes its RubyCocoa implementation. Simply create a new RubyCocoa application in XCode, copy over the English.lproj folder and add the source files defined below.

#
#  WindowController.rb
#

require 'osx/cocoa'

class WindowController < OSX::NSWindowController
  
  ib_outlet :displayTextHere
  ib_outlet :multipleNibTabView
  
  attr_accessor :enterTextNibView
  attr_accessor :setColorNibView

  def init
    @enterTextNibView = nil
    @setColorNibView = nil
    super_init
  end

  def awakeFromNib
# Ensure that the first tab is selected.
    @multipleNibTabView.selectTabViewItemWithIdentifier('1')
  end

  def viewFromNibWithName(nibName)
# Creates an instance of SubViewController which loads the specified nib.
    subViewController = SubViewController.alloc.initWithNibName_andOwner(nibName, self)
    subViewController.view
  end

  def tabView_didSelectTabViewItem(tabView, tabViewItem)
    nibName = nil
# The NSTabView will manage the views being displayed but without the NSTabView, you need to use removeSubview: which releases the view and you need to retain it if you want to use it again later.
    
# Based on the tab selected, we load the appropriate nib and set the tabViewItem's view to the view fromt he nib
    if (tabViewItem.identifier.isEqualToString('1'))
      if !(@enterTextNibView)
        @enterTextNibView = self.viewFromNibWithName('EnterText')
      end
      if (@enterTextNibView)
        tabViewItem.setView(@enterTextNibView)
      end
    end

    if (tabViewItem.identifier.isEqualToString('2'))
      if !(@setColorNibView)
        @setColorNibView = self.viewFromNibWithName('SetColor')
      end
      if (@setColorNibView)
        tabViewItem.setView(@setColorNibView)
      end
    end

  end

  def textField
# This method returns a pointer to the NSTextField on the main window.
    @displayTextHere
  end

end

#
#  SubviewController.rb
#

require 'osx/cocoa'

class SubViewController < OSX::NSObject
  
  ib_outlet :view
  attr_accessor :o wner

  def init
    super_init
  end

  def initWithNibName_andOwner(nibName, owner)
    @owner = owner
    OSX::NSBundle.loadNibNamed_owner(nibName, self)
    init
  end

  def view
    OSX::NSLog('Getting View %@\n', description)
    @view
  end

  def ownerTextField
    @owner.textField
  end

end

#
#  TextFormattinView.rb
#

require 'osx/cocoa'

class TextFormattingView < OSX::NSView
  
  ib_outlet :backgroundColorWell
  ib_outlet :o wner
  ib_outlet :textColorWell

# This method is called when the nib is finished loading.  It makes sure the color wells are set to the colors of the text color and the background color of the NSTextField from the main window.
  def awakeFromNib
    @textColorWell.setColor(@owner.ownerTextField.textColor)
    @backgroundColorWell.setColor(@owner.ownerTextField.backgroundColor)
  end

# This method asks the SubView Controller for a pointer to the NSTextField on the main window and then sets its background color.
  def setBackgroundColor(sender)
    @owner.ownerTextField.setBackgroundColor(sender.color)
  end

# This method asks the SubView Controller for a pointer to the NSTextField on the main window and then sets its text color.
  def setTextColor(sender)
    @owner.ownerTextField.setTextColor(sender.color)
  end

end

#
#  TypeTextHere.rb
#

require 'osx/cocoa'

class TypeTextHere < OSX::NSView
  
  ib_outlet :o wner
  ib_outlet :text

  def sendText(sender)
    @owner.ownerTextField.setStringValue(sender.stringValue)
  end

end

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.