lib/motion-tab/tab_bar.rb in motion-tab-0.2.0 vs lib/motion-tab/tab_bar.rb in motion-tab-0.2.1
- old
+ new
@@ -1,20 +1,33 @@
module MotionTab
class TabBar
class << self
def createTabBarControllerFromData(data)
+ data = self.setTags(data)
+
tabBarController = UITabBarController.alloc.init
tabBarController.viewControllers = self.tabControllersFromData(data)
return tabBarController
end
- def tabBarIcon(icon, tag = 0)
+ def setTags(data)
+ tagNumber = 0
+
+ data.each do |d|
+ d[:tag] = tagNumber
+ tagNumber += 1
+ end
+
+ return data
+ end
+
+ def tabBarIcon(icon, tag)
return UITabBarItem.alloc.initWithTabBarSystemItem(icon, tag: tag)
end
- def tabBarIconCustom(title, imageName, tag = 0)
+ def tabBarIconCustom(title, imageName, tag)
iconImage = UIImage.imageNamed(imageName)
return UITabBarItem.alloc.initWithTitle(title, image:iconImage, tag:tag)
end
def tabControllersFromData(data)
@@ -29,11 +42,12 @@
def controllerFromTabData(tab)
tab[:badgeNumber] = 0 unless tab[:badgeNumber]
tab[:tag] = 0 unless tab[:tag]
- viewController = tab[:viewController].alloc.init
+ viewController = tab[:viewController]
+ viewController = tab[:viewController].alloc.init if tab[:viewController].respond_to?(:alloc)
if tab[:navigationController]
controller = UINavigationController.alloc.initWithRootViewController(viewController)
else
controller = viewController
@@ -48,14 +62,27 @@
def tabBarItem(tab)
title = "Untitled"
title = tab[:title] if tab[:title]
tabBarItem = tabBarIcon(tab[:systemIcon], tab[:tag]) if tab[:systemIcon]
- tabBarItem = tabBarIconCustom(title, tab[:icon]) if tab[:icon]
+ tabBarItem = tabBarIconCustom(title, tab[:icon], tab[:tag]) if tab[:icon]
tabBarItem.badgeValue = tab[:badgeNumber].to_s unless tab[:badgeNumber].nil? || tab[:badgeNumber] <= 0
return tabBarItem
+ end
+
+ def select(tabBarController, title: title)
+ tabBarController.viewControllers.each do |vc|
+ if vc.tabBarItem.title == title
+ tabBarController.selectedIndex = vc.tabBarItem.tag
+ return
+ end
+ end
+ end
+
+ def select(tabBarController, tag: tag)
+ tabBarController.selectedIndex = tag
end
end
end
end
\ No newline at end of file