Permalink
Browse files

Mimicing UITabBarController's API

  • Loading branch information...
1 parent 9228fd4 commit 5a8275aa838f17c3998bf655ed62f6eb3e2fac7e @myell0w myell0w committed Feb 14, 2012
@@ -9,6 +9,11 @@
/* Begin PBXBuildFile section */
9B80B53114EA8F7100B83FA0 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9B80B53014EA8F7100B83FA0 /* Foundation.framework */; };
9B80B53714EA8F7100B83FA0 /* NGVerticalTabBarController.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B80B53614EA8F7100B83FA0 /* NGVerticalTabBarController.m */; };
+ 9B80B54214EA91C400B83FA0 /* NGVerticalTabBarControllerDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 9B80B54114EA91C300B83FA0 /* NGVerticalTabBarControllerDelegate.h */; };
+ 9B80B54514EA9E1000B83FA0 /* NGVerticalTabBarCell.h in Headers */ = {isa = PBXBuildFile; fileRef = 9B80B54314EA9E1000B83FA0 /* NGVerticalTabBarCell.h */; };
+ 9B80B54614EA9E1000B83FA0 /* NGVerticalTabBarCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B80B54414EA9E1000B83FA0 /* NGVerticalTabBarCell.m */; };
+ 9B80B54914EA9E2900B83FA0 /* NGVerticalTabBar.h in Headers */ = {isa = PBXBuildFile; fileRef = 9B80B54714EA9E2900B83FA0 /* NGVerticalTabBar.h */; };
+ 9B80B54A14EA9E2900B83FA0 /* NGVerticalTabBar.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B80B54814EA9E2900B83FA0 /* NGVerticalTabBar.m */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
@@ -17,6 +22,11 @@
9B80B53414EA8F7100B83FA0 /* Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Prefix.pch; sourceTree = "<group>"; };
9B80B53514EA8F7100B83FA0 /* NGVerticalTabBarController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NGVerticalTabBarController.h; sourceTree = "<group>"; };
9B80B53614EA8F7100B83FA0 /* NGVerticalTabBarController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = NGVerticalTabBarController.m; sourceTree = "<group>"; };
+ 9B80B54114EA91C300B83FA0 /* NGVerticalTabBarControllerDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NGVerticalTabBarControllerDelegate.h; sourceTree = "<group>"; };
+ 9B80B54314EA9E1000B83FA0 /* NGVerticalTabBarCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NGVerticalTabBarCell.h; sourceTree = "<group>"; };
+ 9B80B54414EA9E1000B83FA0 /* NGVerticalTabBarCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NGVerticalTabBarCell.m; sourceTree = "<group>"; };
+ 9B80B54714EA9E2900B83FA0 /* NGVerticalTabBar.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NGVerticalTabBar.h; sourceTree = "<group>"; };
+ 9B80B54814EA9E2900B83FA0 /* NGVerticalTabBar.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NGVerticalTabBar.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -61,6 +71,11 @@
children = (
9B80B53514EA8F7100B83FA0 /* NGVerticalTabBarController.h */,
9B80B53614EA8F7100B83FA0 /* NGVerticalTabBarController.m */,
+ 9B80B54714EA9E2900B83FA0 /* NGVerticalTabBar.h */,
+ 9B80B54814EA9E2900B83FA0 /* NGVerticalTabBar.m */,
+ 9B80B54314EA9E1000B83FA0 /* NGVerticalTabBarCell.h */,
+ 9B80B54414EA9E1000B83FA0 /* NGVerticalTabBarCell.m */,
+ 9B80B54114EA91C300B83FA0 /* NGVerticalTabBarControllerDelegate.h */,
9B80B53314EA8F7100B83FA0 /* Supporting Files */,
);
path = NGVerticalTabBarController;
@@ -81,6 +96,9 @@
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
+ 9B80B54214EA91C400B83FA0 /* NGVerticalTabBarControllerDelegate.h in Headers */,
+ 9B80B54514EA9E1000B83FA0 /* NGVerticalTabBarCell.h in Headers */,
+ 9B80B54914EA9E2900B83FA0 /* NGVerticalTabBar.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -136,6 +154,8 @@
buildActionMask = 2147483647;
files = (
9B80B53714EA8F7100B83FA0 /* NGVerticalTabBarController.m in Sources */,
+ 9B80B54614EA9E1000B83FA0 /* NGVerticalTabBarCell.m in Sources */,
+ 9B80B54A14EA9E2900B83FA0 /* NGVerticalTabBar.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -0,0 +1,13 @@
+//
+// NGVerticalTabBar.h
+// NGVerticalTabBarController
+//
+// Created by Tretter Matthias on 14.02.12.
+// Copyright (c) 2012 NOUS Wissensmanagement GmbH. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+
+@interface NGVerticalTabBar : UITableView
+
+@end
@@ -0,0 +1,20 @@
+#import "NGVerticalTabBar.h"
+
+@implementation NGVerticalTabBar
+
+////////////////////////////////////////////////////////////////////////
+#pragma mark - Lifecycle
+////////////////////////////////////////////////////////////////////////
+
+- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
+ if ((self = [super initWithFrame:frame style:style])) {
+ self.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin;
+ self.showsHorizontalScrollIndicator = NO;
+ self.showsVerticalScrollIndicator = NO;
+ self.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
+ }
+
+ return self;
+}
+
+@end
@@ -0,0 +1,13 @@
+//
+// NGVerticalTabBarCell.h
+// NGVerticalTabBarController
+//
+// Created by Tretter Matthias on 14.02.12.
+// Copyright (c) 2012 NOUS Wissensmanagement GmbH. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+
+@interface NGVerticalTabBarCell : UITableViewCell
+
+@end
@@ -0,0 +1,21 @@
+#import "NGVerticalTabBarCell.h"
+
+@implementation NGVerticalTabBarCell
+
+- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
+{
+ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
+ if (self) {
+ // Initialization code
+ }
+ return self;
+}
+
+- (void)setSelected:(BOOL)selected animated:(BOOL)animated
+{
+ [super setSelected:selected animated:animated];
+
+ // Configure the view for the selected state
+}
+
+@end
@@ -6,6 +6,29 @@
// Copyright (c) 2012 NOUS Wissensmanagement GmbH. All rights reserved.
//
-@interface NGVerticalTabBarController : NSObject
+@protocol NGVerticalTabBarControllerDelegate;
+@class NGVerticalTabBar;
+
+/** NGVerticalTabBarController is a vertical TabBar displayed on the left side of the device */
+@interface NGVerticalTabBarController : UIViewController
+
+/** An array of the view controllers displayed by the tab bar */
+@property (nonatomic, copy) NSArray *viewControllers;
+/** The index of the view controller associated with the currently selected tab item. */
+@property (nonatomic, assign) NSUInteger selectedIndex;
+/** The view controller associated with the currently selected tab item. */
+@property (nonatomic, unsafe_unretained) UIViewController *selectedViewController;
+
+/** The tab bar controller’s delegate object. */
+@property (nonatomic, unsafe_unretained) id<NGVerticalTabBarControllerDelegate> delegate;
+
+/** The tableView used to display all tab bar elements */
+@property (nonatomic, strong, readonly) NGVerticalTabBar *tabBar;
+
+/** Sets the view controllers of the tab bar controller. */
+- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated;
+
+/** The designated initializer. */
+- (id)initWithViewControllers:(NSArray *)viewControllers;
@end
@@ -1,14 +1,192 @@
#import "NGVerticalTabBarController.h"
+#import "NGVerticalTabBarControllerDelegate.h"
+#import "NGVerticalTabBar.h"
+#import "NGVerticalTabBarCell.h"
+
+// the default width of the tabBar
+#define kNGTabBarControllerDefaultWidth 150.f
+
+
+@interface NGVerticalTabBarController () <UITableViewDataSource, UITableViewDelegate> {
+ // re-defined as mutable
+ NSMutableArray *viewControllers_;
+
+ // flags for methods implemented in the delegate
+ struct {
+ unsigned int widthOfTabBar:1;
+ unsigned int shouldSelectViewController:1;
+ unsigned int didSelectViewController:1;
+ } delegateFlags_;
+}
+
+// re-defined as read/write
+@property (nonatomic, strong, readwrite) NGVerticalTabBar *tabBar;
+
+- (void)updateUI;
+
+- (CGFloat)askDelegateForWidthOfTabBar;
+- (NGVerticalTabBarCell *)askDelegateForCellOfViewController:(UIViewController *)viewController atIndex:(NSUInteger)index;
+- (BOOL)askDelegateIfWeShouldSelectViewController:(UIViewController *)viewController atIndex:(NSUInteger)index;
+- (void)callDelegateDidSelectViewController:(UIViewController *)viewController atIndex:(NSUInteger)index;
+
+@end
@implementation NGVerticalTabBarController
-- (id)init {
- self = [super init];
- if (self) {
- // Initialization code here.
+@synthesize viewControllers = viewControllers_;
+@synthesize selectedIndex = selectedIndex_;
+@synthesize delegate = delegate_;
+@synthesize tabBar = tabBar_;
+
+////////////////////////////////////////////////////////////////////////
+#pragma mark - Lifecycle
+////////////////////////////////////////////////////////////////////////
+
+- (id)initWithViewControllers:(NSArray *)viewControllers {
+ if ((self = [super initWithNibName:nil bundle:nil])) {
+ if (viewControllers.count > 0) {
+ viewControllers_ = [NSMutableArray arrayWithArray:viewControllers];
+ selectedIndex_ = 0;
+ } else {
+ viewControllers_ = [NSMutableArray array];
+ selectedIndex_ = NSNotFound;
+ }
}
return self;
}
+- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
+ return [self initWithViewControllers:nil];
+}
+
+////////////////////////////////////////////////////////////////////////
+#pragma mark - UIViewController
+////////////////////////////////////////////////////////////////////////
+
+- (void)viewDidLoad {
+ [super viewDidLoad];
+
+ CGFloat width = [self askDelegateForWidthOfTabBar];
+ self.tabBar = [[NGVerticalTabBar alloc] initWithFrame:CGRectMake(0.f, 0.f, width, self.view.bounds.size.height) style:UITableViewStylePlain];
+
+ self.tabBar.dataSource = self;
+ self.tabBar.delegate = self;
+}
+
+- (void)viewDidUnload {
+ self.tabBar.dataSource = nil;
+ self.tabBar.delegate = nil;
+ self.tabBar = nil;
+
+ [super viewDidUnload];
+}
+
+- (void)viewWillAppear:(BOOL)animated {
+ [super viewWillAppear:animated];
+
+ [self.tabBar reloadData];
+}
+
+////////////////////////////////////////////////////////////////////////
+#pragma mark - NGVerticalTabBarController
+////////////////////////////////////////////////////////////////////////
+
+- (void)setDelegate:(id<NGVerticalTabBarControllerDelegate>)delegate {
+ if (delegate != delegate_) {
+ delegate_ = delegate;
+
+ // update delegate flags
+ delegateFlags_.widthOfTabBar = [delegate respondsToSelector:@selector(widthOfTabBarOfVerticalTabBarController:)];
+ delegateFlags_.shouldSelectViewController = [delegate respondsToSelector:@selector(verticalTabBarController:shouldSelectViewController:atIndex:)];
+ delegateFlags_.didSelectViewController = [delegate respondsToSelector:@selector(verticalTabBarController:didSelectViewController:atIndex:)];
+ }
+}
+
+- (UIViewController *)selectedViewController {
+ NSAssert(self.selectedIndex < self.viewControllers.count, @"Selected index is invalid");
+
+ id selectedViewController = [self.viewControllers objectAtIndex:self.selectedIndex];
+
+ if (selectedViewController == [NSNull null]) {
+ return nil;
+ }
+
+ return selectedViewController;
+}
+
+- (void)setSelectedViewController:(UIViewController *)selectedViewController {
+ NSAssert([self.viewControllers containsObject:selectedViewController], @"View controller must be a part of the TabBar");
+
+ // updates the UI
+ self.selectedIndex = [self.viewControllers indexOfObject:selectedViewController];
+}
+
+- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated {
+ if (animated) {
+ // TODO:
+ } else {
+ self.viewControllers = viewControllers;
+ }
+}
+
+- (void)setViewControllers:(NSArray *)viewControllers {
+ if (viewControllers != viewControllers_) {
+ viewControllers_ = [NSMutableArray arrayWithArray:viewControllers];
+
+ [self updateUI];
+ }
+}
+
+- (void)setSelectedIndex:(NSUInteger)selectedIndex {
+ if (selectedIndex != selectedIndex_) {
+ selectedIndex_ = selectedIndex;
+
+ [self updateUI];
+ }
+}
+
+////////////////////////////////////////////////////////////////////////
+#pragma mark - UITableViewDataSource
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+#pragma mark - UITableViewDelegate
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+#pragma mark - Private
+////////////////////////////////////////////////////////////////////////
+
+- (void)updateUI {
+ // TODO: Update UI depending on current state
+}
+
+- (CGFloat)askDelegateForWidthOfTabBar {
+ if (delegateFlags_.widthOfTabBar) {
+ return [self.delegate widthOfTabBarOfVerticalTabBarController:self];
+ }
+
+ return kNGTabBarControllerDefaultWidth;
+}
+
+- (NGVerticalTabBarCell *)askDelegateForCellOfViewController:(UIViewController *)viewController atIndex:(NSUInteger)index {
+ return [self.delegate verticalTabBarController:self cellForViewController:viewController atIndex:index];
+}
+
+- (BOOL)askDelegateIfWeShouldSelectViewController:(UIViewController *)viewController atIndex:(NSUInteger)index {
+ if (delegateFlags_.shouldSelectViewController) {
+ return [self.delegate verticalTabBarController:self shouldSelectViewController:viewController atIndex:index];
+ }
+
+ // default: the view controller can be selected
+ return YES;
+}
+
+- (void)callDelegateDidSelectViewController:(UIViewController *)viewController atIndex:(NSUInteger)index {
+ if (delegateFlags_.didSelectViewController) {
+ [self.delegate verticalTabBarController:self didSelectViewController:viewController atIndex:index];
+ }
+}
+
@end
@@ -0,0 +1,36 @@
+//
+// NGVerticalTabBarControllerDelegate.h
+// NGVerticalTabBarController
+//
+// Created by Tretter Matthias on 14.02.12.
+// Copyright (c) 2012 NOUS Wissensmanagement GmbH. All rights reserved.
+//
+
+@class NGVerticalTabBarController;
+@class NGVerticalTabBarCell;
+
+@protocol NGVerticalTabBarControllerDelegate <NSObject>
+
+@required
+
+/** Asks the delegate for a customized cell */
+- (NGVerticalTabBarCell *)verticalTabBarController:(NGVerticalTabBarController *)tabBarController
+ cellForViewController:(UIViewController *)viewController
+ atIndex:(NSUInteger)index;
+
+@optional
+
+/** Asks the delegate for the width of the UITableView that acts as the tabBar */
+- (CGFloat)widthOfTabBarOfVerticalTabBarController:(NGVerticalTabBarController *)tabBarController;
+
+/** Asks the delegate whether the specified view controller should be made active. */
+- (BOOL)verticalTabBarController:(NGVerticalTabBarController *)tabBarController
+ shouldSelectViewController:(UIViewController *)viewController
+ atIndex:(NSUInteger)index;
+
+/** Tells the delegate that the user selected an item in the tab bar. */
+- (void)verticalTabBarController:(NGVerticalTabBarController *)tabBarController
+ didSelectViewController:(UIViewController *)viewController
+ atIndex:(NSUInteger)index;
+
+@end

0 comments on commit 5a8275a

Please sign in to comment.