2011-07-31 51 views
0

在我的標籤欄的標籤,似乎並不被顯示出來。這裏是我的應用程序代碼代表:的iOS(iPhone/iPad的)SDK - 標籤在標籤欄沒有顯示出來

(應用程序名稱)AppDelegate.h:

#import <UIKit/UIKit.h> 

    @class TwitterViewContoller; 

    @interface <appname>AppDelegate : NSObject <UIApplicationDelegate> { 
     UIWindow *window; 
     UITabBarController *rootController; 
     TwitterViewContoller *viewController; 
     NSMutableData *responseData; 
     NSMutableArray *tweets; 
    } 

    @property (nonatomic, retain) IBOutlet UIWindow *window; 
    @property (nonatomic, retain) IBOutlet UITabBarController *rootController; 
    @property (nonatomic, retain) IBOutlet TwitterViewContoller *viewController; 
    @property (nonatomic, retain) NSMutableArray *tweets; 

    @end 

(應用程序名稱)AppDelegate.m:

#import "<appname>AppDelegate.h" 
    #import "TwitterViewContoller.h" 
    #import "SBJson.h" 

    @implementation <appname>AppDelegate 

    @synthesize window = _window; 
    @synthesize rootController; 
    @synthesize viewController; 
    @synthesize tweets; 

    #pragma mark - 
    #pragma mark Application lifecycle 

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
     // Override point for customization after application launch. 
     CGFloat width = self.rootController.view.bounds.size.width; 
     CGFloat height = self.rootController.view.bounds.size.height; 
     UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, width, height)]; 
     UIImage *imageView = [UIImage imageNamed:@"TabBarBackground.png"]; 
     UIColor *kMainColor = [[UIColor alloc] initWithPatternImage:imageView]; 

     [v setBackgroundColor:kMainColor]; 
     [kMainColor release]; 

     [self.rootController.tabBar insertSubview:v atIndex:0]; 
     [imageView release]; 
     [v release]; 

     responseData = [[NSMutableData data] retain]; 
     tweets = [NSMutableArray array]; 

     NSURLRequest *request = [NSURLRequest requestWithURL: 
         [NSURL URLWithString:@"http://api.twitter.com/1/statuses/user_timeline.json?screen_name=USER_NAME_ID"]]; 

     [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
       NSAssert(nil != self.rootController, @"tab bar controller not hooked up!"); 

     self.viewController = [[[TwitterViewContoller alloc] initWithNibName:@"TwitterViewContoller_iPhone" bundle:nil] autorelease]; 
     self.rootController.viewControllers = [NSArray arrayWithObject:self.viewController]; 

     // this is now the Right Way to set the root view for your app, in iOS 4.0 and later 
     self.window.rootViewController = self.rootController; 

     [self.window makeKeyAndVisible]; 
     return YES; 
    } 

    #pragma mark NSURLConnection delegate methods 
    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
     [responseData setLength:0]; 
    } 

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
     [responseData appendData:data]; 
    } 

    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
    } 

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
     [connection release]; 
     NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
     [responseData release]; 

     NSArray *allTweets = [responseString JSONValue]; 
     [viewController setTweets:allTweets]; 

    } 

    - (void)dealloc { 
     [_window release]; 
     [rootController release]; 
     [viewController release]; 
     [super dealloc]; 
    } 

    @end 

我相信這個問題是在application didFinishLaunchingWithOptions:作爲近期唯一我對App Delegate所做的更改一直是這些行:

NSAssert(nil != self.rootController, @"tab bar controller not hooked up!"); 

    self.viewController = [[[TwitterViewContoller alloc] initWithNibName:@"TwitterViewContoller_iPhone" bundle:nil] autorelease]; 
    self.rootController.viewControllers = [NSArray arrayWithObject:self.viewController]; 

    // this is now the Right Way to set the root view for your app, in iOS 4.0 and later 
    self.window.rootViewController = self.rootController; 
    [self.window makeKeyAndVisible]; 

(編輯:刪除上述第二和第三個行解決了這個問題,但我需要他們)。有任何想法嗎?

回答

0

製作其他ViewControllers的屬性並將它們合成到實現文件(.m)中。此外它們導入您的AppDelegate.m文件。這裏是你的頭文件(.h)文件將是什麼樣子(rootController是你的TabBarController):

#import <UIKit/UIKit.h> 

    @class TwitterViewContoller; 
    @class OtherViewController; 

    @interface <appname>AppDelegate : NSObject <UIApplicationDelegate> { 
     UIWindow *window; 
     UITabBarController *rootController; 
     TwitterViewContoller *viewController; 
     OtherViewController *viewController1; 
    } 

    @property (nonatomic, retain) IBOutlet UIWindow *window; 
    @property (nonatomic, retain) IBOutlet UITabBarController *rootController; 
    @property (nonatomic, retain) IBOutlet TwitterViewContoller *viewController; 
    @property (nonatomic, retain) IBOutlet OtherViewController *viewController1; 

    @end 

以及實現的前幾行(.M)文件:

#import "<appname>AppDelegate.h" 
#import "TwitterViewContoller.h" 
#import "OtherViewController.h" 

@implementation <appname>AppDelegate 

@synthesize window = _window; 
@synthesize rootController; 
@synthesize viewController; 
@synthesize viewController1; 

添加額外allocs和inits您的所有ViewControllers:

self.viewController = [[[TwitterViewContoller alloc] initWithNibName:@"TwitterViewContoller_iPhone" bundle:nil] autorelease]; 
self.viewController1 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController_iPhone" bundle:nil] autorelease]; 

然後替換此:

self.rootController.viewControllers = [NSArray arrayWithObject:self.viewController]; 

與此:

self.rootController.viewControllers = [NSArray arrayWithObjects:self.viewController, self.viewController1, nil]; 

這使您可以不止一個的UIViewController添加到您的TabBar控制器。

然後,當然,釋放:

[viewController release]; 
[viewController1 release]; 

而且每一類你ViewControllers下,下-initWithNibName方法添加這個if (self)語句中:

UITabBarItem *tabBarItem = [self tabBarItem]; 
UIImage *tabBarImage = [UIImage imageNamed:@"IMAGE NAME.png"]; 
[tabBarItem setImage:tabBarImage]; 
[tabBarItem setTitle:@"TITLE GOES HERE"]; 

運行你的應用程序,它應該是現在的工作!