Module: ProMotion::ScreenTabs

Included in:
Delegate, ScreenModule
Defined in:
lib/ProMotion/screen_helpers/screen_tabs.rb

Instance Method Summary collapse

Instance Method Details

#create_tab_bar_icon(icon, tag) ⇒ Object



54
55
56
# File 'lib/ProMotion/screen_helpers/screen_tabs.rb', line 54

def create_tab_bar_icon(icon, tag)
  return UITabBarItem.alloc.initWithTabBarSystemItem(icon, tag: tag)
end

#create_tab_bar_icon_custom(title, image_name, tag) ⇒ Object



58
59
60
61
# File 'lib/ProMotion/screen_helpers/screen_tabs.rb', line 58

def create_tab_bar_icon_custom(title, image_name, tag)
  icon_image = UIImage.imageNamed(image_name)
  return UITabBarItem.alloc.initWithTitle(title, image:icon_image, tag:tag)
end

#create_tab_bar_item(tab = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ProMotion/screen_helpers/screen_tabs.rb', line 63

def create_tab_bar_item(tab={})
  title = "Untitled"
  title = tab[:title] if tab[:title]
  tab[:tag] ||= @current_tag ||= 0
  @current_tag = tab[:tag] + 1

  tab_bar_item = create_tab_bar_icon(tab[:system_icon], tab[:tag]) if tab[:system_icon]
  tab_bar_item = create_tab_bar_icon_custom(title, tab[:icon], tab[:tag]) if tab[:icon]

  tab_bar_item.badgeValue = tab[:badge_number].to_s unless tab[:badge_number].nil? || tab[:badge_number] <= 0

  return tab_bar_item
end

#open_tab(tab) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/ProMotion/screen_helpers/screen_tabs.rb', line 43

def open_tab(tab)
  if tab.is_a? String
    return self.select(self.tab_bar, title: tab)
  elsif tab.is_a? Numeric
    tab_bar_controller.selectedIndex = tab
    return tab_bar_controller.viewControllers[tab]
  else
    $stderr.puts "Unable to open tab #{tab.to_s} because it isn't a string."
  end
end

#open_tab_bar(*screens) ⇒ UITabBarController

Open a UITabBarController with the specified screens as the root view controller of the current app.

Parameters:

  • A (Array)

    comma-delimited list of screen classes or instances.

Returns:

  • (UITabBarController)


34
35
36
37
38
39
40
41
# File 'lib/ProMotion/screen_helpers/screen_tabs.rb', line 34

def open_tab_bar(*screens)
  tab_bar = tab_bar_controller(*screens)

  a = self.respond_to?(:load_root_screen) ? self : UIApplication.sharedApplication.delegate

  a.load_root_screen(tab_bar)
  tab_bar
end

#replace_current_item(tab_bar_controller, view_controller: vc) ⇒ Object



89
90
91
92
93
# File 'lib/ProMotion/screen_helpers/screen_tabs.rb', line 89

def replace_current_item(tab_bar_controller, view_controller: vc)
  controllers = NSMutableArray.arrayWithArray(tab_bar_controller.viewControllers)
  controllers.replaceObjectAtIndex(tab_bar_controller.selectedIndex, withObject: vc)
  tab_bar_controller.viewControllers = controllers
end

#select(tab_bar_controller, title: title) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ProMotion/screen_helpers/screen_tabs.rb', line 77

def select(tab_bar_controller, title: title)
  root_controller = nil
  tab_bar_controller.viewControllers.each do |vc|
    if vc.tabBarItem.title == title
      tab_bar_controller.selectedViewController = vc
      root_controller = vc
      break
    end
  end
  root_controller
end

#tab_bar_controller(*screens) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ProMotion/screen_helpers/screen_tabs.rb', line 3

def tab_bar_controller(*screens)
  tab_bar_controller = UITabBarController.alloc.init

  view_controllers = []
  tag_index = 0

  screens.map! { |s| s.respond_to?(:new) ? s.new : s } # Initialize any classes

  screens.each do |s|
    s = s.new if s.respond_to?(:new)

    s.tabBarItem.tag = tag_index

    s.parent_screen = self if self.is_a?(UIViewController) && s.respond_to?("parent_screen=")
    s.tab_bar = tab_bar_controller if s.respond_to?("tab_bar=")

    view_controllers << s.pm_main_controller

    tag_index += 1

    s.on_load if s.respond_to?(:on_load)
  end

  tab_bar_controller.viewControllers = view_controllers
  tab_bar_controller
end