TabBar支持Three20 iPhone照片库

时间:2011-01-11 07:46:39

标签: iphone three20 tabbar photo-gallery

我浏览了this教程并为iPhone创建了一个照片库。现在我想将它添加到我的TabBar项目中。我已经听说过,Three20不支持XIB,所以我将整个标签栏设置改为以编程方式。我想我离最终的解决方案并不太远。

我能够让照片库在一个标签中工作但没有功能(点击图片 - >它打开等)。页面顶部没有导航,可以引导您进入详细图像页面。当我从app delegate中的didFinishLaunchingWithOptions-method中删除它时,我遇到了这个问题:

// Override point for customization after application launch
TTNavigator* navigator = [TTNavigator navigator];
TTURLMap* map = navigator.URLMap;
[map from:@"demo://album" toViewController:  [AlbumController class]];

[navigator openURLAction:[TTURLAction actionWithURLPath:@"demo://album"]];
return YES;

我不得不将其删除,否则整个标签栏都不会显示。照片库使用整个屏幕。我不确定它是否未显示或未加载。我也尝试过:

tabbar.hidesBottomBarWhenPushed = NO;

但这根本不起作用。我试图在没有结果的情况下将TTNavigator代码添加到AlbumController本身的loadView(),viewDidLoad()和init()。有谁知道我必须把它放在哪里才能使它工作?

我的AlbumController.h:

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

@interface AlbumController : TTThumbsViewController {
    // images
    NSMutableArray *images;

    // parser
    NSXMLParser * rssParser;
    NSMutableArray * stories;
    NSMutableDictionary * item;
    NSString * currentElement;
    NSMutableString * currentImage;
    NSMutableString * currentCaption;
}

@property (nonatomic, retain) NSMutableArray *images;

@end

我执行了didFinishLaunchingWithOptions-method:

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

    // set up tab bar controller
    tabBarController = [[UITabBarController alloc] init];        
    albumController = [[AlbumController alloc] init];  
    firstViewController = [[FirstViewController alloc] init];  
    secondViewController = [[SecondViewController alloc] init];  
    firstViewController.delegateRef = self;
    tabBarController.viewControllers = [NSArray arrayWithObjects:firstViewController, secondViewController, albumController, nil];  
    [window addSubview:tabBarController.view];                                             
    [window makeKeyAndVisible]; 

    // Override point for customization after application launch
    TTNavigator* navigator = [TTNavigator navigator];
    TTURLMap* map = navigator.URLMap;
    [map from:@"demo://album" toViewController:  [AlbumController class]];
    [navigator openURLAction:[TTURLAction actionWithURLPath:@"demo://album"]];
    return YES;
}

谢谢你们,干杯,dooonot

3 个答案:

答案 0 :(得分:6)

好的,在Bryan的帮助下,我能够在标签栏应用程序中运行照片库。我看到很多人在那里寻找这个解决方案,所以我尽力解释它。

似乎无法在Interface Builder中使用Three20,因此您必须手动设置标签栏应用程序。这是我的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

请确保您创建一个新的UITabBarController以及所有ViewControllers。让我们继续我的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

请注意,您不需要教程中的TTNavigator。现在我们必须得到我们的照相馆。我像在教程中一样在AlbumController中构建它。这是我的AlbumController.h:

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

@interface AlbumController : TTThumbsViewController {

}

@property (nonatomic, retain) NSMutableArray *images;

@end

您可以在上面提到的教程中找到AlbumController的实现。现在是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

如上面的问题描述中所述,我的照片库始终使用全屏。这很糟糕,因为您无法再使用标签栏图标了。为此你必须添加

self.hidesBottomBarWhenPushed=NO;

到你上面的AlbumController-init-method中提到的init()方法。

Sooo,这就是它。我真的希望有人可以重新使用我的解决方案。再次感谢布莱恩。

干杯队员, doonot

PS:我在github上创建了一个项目。您可以下载示例应用here

答案 1 :(得分:0)

试试这个:

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;

答案 2 :(得分:0)

您可以使用TTNavigatorDemo示例了解如何将其与标签栏控制器一起使用。