Three20 iPhone照片库的TabBar支持

4
我参考了这篇教程,为iPhone创建了一个照片库。现在我想将其添加到我的TabBar项目中。我已经听说Three20不支持XIB,所以我改变了整个选项卡栏的设置方式,采用程序化方法。我认为我离最终解决方案并不远。
我能够在一个选项卡中使照片库正常工作,但是没有功能(点击图片 - >它会打开等)。页面上没有导航栏,无法进入详细图片页面。我遇到了这个问题,当我从应用程序委托的didFinishLaunchingWithOptions方法中删除了它时。
// 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方法的实现:

- (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个回答

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()方法中添加以下内容。

所以,基本上就是这样。我真的希望有人能够重复使用我的解决方案。再次感谢Bryan。

大家好, doonot

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


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;

嗨Bryan,非常感谢你的回答。我尝试了你的代码,但是我只得到一个白屏和页面底部的黑色条纹。我尝试修改代码,但是无法解决问题。还有其他想法吗?:( - nimrod
我在 Twitter 上收到了你的推文 :). 你说 AlbumController/ActionController 需要是 UITabViewController。我正在使用上述教程中的 AlbumController 代码。该教程使用 TTThumbsViewController,因为我想像照片应用程序中一样显示这些缩略图。在选项卡图标下显示“普通”视图根本不是问题... - nimrod
就我目前所知,我认为AlbumController被正确调用了,但是我认为它隐藏了页面底部的选项卡栏! 我尝试过[self.hidesBottomBarWhenPushed = NO]; 和[self.wantsFullScreenLayout = NO]; 但没有成功! - nimrod

0

您可以使用TTNavigatorDemo示例来学习如何与选项卡控制器一起使用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接