Class: RMXSegmentedController

Inherits:
RMXViewController show all
Defined in:
lib/motion/RMXSegmentedController.rb

Instance Method Summary collapse

Methods inherited from RMXViewController

#didReceiveMemoryWarning, #init, #refresh, #viewDidAppear, #viewDidDisappear, #viewDidLoad, #viewWillAppear, #viewWillDisappear

Methods included from RMXSetAttributes

included

Methods included from RMXKeyboardHelpers

#keyboardChanged, #keyboardChangedInternal, #keyboard_proxy, #keyboard_proxy_constraints, #listenForKeyboardChanged

Methods included from RMXViewControllerPresentation

included

Methods included from RMXViewControllerPresentation::FactoryMethods

#dismiss, #present

Methods included from RMXCommonMethods

#dealloc, #description, #inspect

Instance Method Details

#cleanup_activeObject



56
57
58
59
60
61
62
63
# File 'lib/motion/RMXSegmentedController.rb', line 56

def cleanup_active
  if @active_controller
    @active_controller.willMoveToParentViewController(nil)
    @active_controller.view.removeFromSuperview
    @active_controller.removeFromParentViewController
    @active_controller = nil
  end
end

#control_change(sender) ⇒ Object



30
31
32
# File 'lib/motion/RMXSegmentedController.rb', line 30

def control_change(sender)
  self.selectedIndex = sender.selectedSegmentIndex
end

#loadedObject



9
10
11
12
# File 'lib/motion/RMXSegmentedController.rb', line 9

def loaded
  navigationItem.titleView = @control
  self.selectedIndex = 0
end

#prepareObject



3
4
5
6
7
# File 'lib/motion/RMXSegmentedController.rb', line 3

def prepare
  @controller_indexes = []
  @control = UISegmentedControl.new
  @control.addTarget(self, action:'control_change:', forControlEvents:UIControlEventValueChanged)
end

#rmx_deallocObject



65
66
67
68
# File 'lib/motion/RMXSegmentedController.rb', line 65

def rmx_dealloc
  cleanup_active
  @controller_indexes = nil
end

#segments=(segments) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/motion/RMXSegmentedController.rb', line 14

def segments=(segments)
  @controller_indexes.clear
  @control.removeAllSegments
  if segments
    segments.each_with_index do |segment, i|
      if segment[:title]
        @control.insertSegmentWithTitle(segment[:title], atIndex:i, animated:false)
      elsif segment[:image]
        @control.insertSegmentWithImage(segment[:image], atIndex:i, animated:false)
      end
      @controller_indexes[i] = segment[:controller]
    end
  end
  @control.sizeToFit
end

#selectedIndex=(i) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/motion/RMXSegmentedController.rb', line 34

def selectedIndex=(i)
  cleanup_active
  @control.selectedSegmentIndex = @control.numberOfSegments > i ? i : UISegmentedControlNoSegment
  @active_controller = @controller_indexes[i]
  if @active_controller
    addChildViewController(@active_controller)
    RMX::Layout.new do |layout|
      layout.view = view
      layout.subviews = {
        "content" => @active_controller.view
      }
      layout.eqs %Q{
        content.left == 0
        content.right == 0
        content.top == 0
        content.bottom == 0
      }
    end
    @active_controller.didMoveToParentViewController(self)
  end
end