TabBar Support of Three20 iPhone Photo Gallery

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 15:27:09

Ok guys, with help of Bryan I was able to get the photo gallery running in a tab bar application. I have seen so many people out there looking for this solution so I try to explain it as good as I can.

Seems like it is not possible to use Three20 with Interface Builder, so you have to set up your tab bar application manually. This is my Three20PhotoGalleryAppDelegate.h:

#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#import "AlbumController.h"
#import "SecondViewController.h"
#import "FirstViewController.h"

@class TabBarAppViewController;
@class AlbumController;
@class SecondViewController;
@class FirstViewController;

@interface Three20PhotoGalleryAppDelegate : NSObject <UIApplicationDelegate> {

    UIWindow *window;
    UITabBarController *tabBarController;
    AlbumController *albumController;
    FirstViewController *firstViewController;
    SecondViewController *secondViewController;

@private
    NSManagedObjectContext *managedObjectContext_;
    NSManagedObjectModel *managedObjectModel_;
    NSPersistentStoreCoordinator *persistentStoreCoordinator_;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) UITabBarController *tabBarController;
@property (nonatomic, retain) AlbumController *albumController;
@property (nonatomic, retain) SecondViewController *secondViewController;
@property (nonatomic, retain) FirstViewController *firstViewController;

@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;

- (NSURL *)applicationDocumentsDirectory;
- (void)saveContext;

@end

Please make sure that you create a new UITabBarController as well as all your ViewControllers. Let's continue with my Three20PhotoGalleryAppDelegate.m:

#import "Three20PhotoGalleryAppDelegate.h"
#import "AlbumController.h"
#import "SecondViewController.h"
#import "FirstViewController.h"
#import <Three20/Three20.h>

@implementation Three20PhotoGalleryAppDelegate

@synthesize window;
@synthesize albumController;
@synthesize firstViewController;
@synthesize secondViewController;
@synthesize tabBarController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // set up tab bar controller manually
    tabBarController = [[UITabBarController alloc] init];        
    albumController = [[AlbumController alloc] init];  
    firstViewController = [[FirstViewController alloc] init];  
    secondViewController = [[SecondViewController alloc] init];  

    /* This is the essential part of the solution. You have to add the albumController to a 
    new  navigation controller and init it as RootViewController*/
    UINavigationController* navController = [[[UINavigationController alloc] initWithRootViewController:albumController] autorelease];

    // now add all controllers to the tabBarController
    tabBarController.viewControllers = [NSArray arrayWithObjects:firstViewController, secondViewController, navController, nil];    

    [window addSubview:tabBarController.view];                                             
    [window makeKeyAndVisible];  
}

- (BOOL)application:(UIApplication*)application handleOpenURL:(NSURL*)URL {
    TTOpenURL([URL absoluteString]);
    return YES;
}

- (void)dealloc {
    [tabBarController release];
    [window release];
    [super dealloc];
}

@end

Please note that you don't need the TTNavigator stuff from the tutorial. Now we have to get our photogallery some how. I built it up in AlbumController like in the tutorial. This is my AlbumController.h:

#import <Foundation/Foundation.h>
#import <Three20/Three20.h>

@interface AlbumController : TTThumbsViewController {

}

@property (nonatomic, retain) NSMutableArray *images;

@end

You can find the implementation of the AlbumController in the tutorial mentioned above. Now the AlbumController.m:

#import "AlbumController.h"
#import "PhotoSource.h"
#import "Photo.h"

@implementation AlbumController
@synthesize images;

- (id)init
{
    if (self = [super init]) 
    {
        // Initialization code
        self.title = @"Photo Gallery";
        self.hidesBottomBarWhenPushed=NO;
    }
    return self;
}


- (void)viewDidLoad {

    [self createPhotos]; // method to set up the photos array
    self.photoSource = [[PhotoSource alloc]
                        initWithType:PhotoSourceNormal
                        title:@"All in Vain"
                        photos:images
                        photos2:nil];
}

-(void)createPhotos {
    // your independent implementation
}

@end

As described in the problem description above, my photo gallery always used the full screen. This is bad because you cannot use your tab bar icons anymore. For this you have to add

self.hidesBottomBarWhenPushed=NO;

to your init() method like mentioned in the AlbumController-init-method above.

Sooo, that's pretty much it. I really hope someone can re-use my solution. Thanks again to Bryan.

Cheers guys, doonot

PS: I have created a project on github. You can download the sample app here.

Try This:

tBarController = [[UITabBarController alloc] init];
 actionController = [[ActionController alloc] initWithNibName:nil bundle:nil];
    // Override point for customization after application launch.
    TTNavigator* navigator = [TTNavigator navigator];
 TTURLMap* map = navigator.URLMap;
 [map from:@"demo://album" toViewController:tBarController];
 [tBarController setViewControllers:
     [NSArray arrayWithObjects:actionController,nil]];
 [navigator openURLAction:[TTURLAction actionWithURLPath:@"demo://album"]];

 [self.window addSubview:tBarController.view];
 [self.window makeKeyAndVisible];

    return YES;

You can use the TTNavigatorDemo sample to learn how to use it with Tab Bar Controllers.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!