开发者

iOS (iPhone/iPad) SDK - Tabs in Tab Bar not showing up

The Tabs in my Tab Bar don't seem to be showing up. Here is my App Delegate code:

(appname)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

(appname)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 = [UII开发者_JS百科mage 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

I believe the problem is in application didFinishLaunchingWithOptions: as the only recent changes I have made to App Delegate have been these lines:

    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];

(EDIT: removing the 2nd and 3rd line above fixes the problem but I need to have them). Any ideas?


Make a Property of your other ViewControllers and synthesize them in your implementation file (.m). Also import them in your AppDelegate.m file. Here is what your header (.h) file will look like (rootController is your 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

And the first few lines of your implementation (.m) file:

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

@implementation <appname>AppDelegate

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

Add additional allocs and inits for all your ViewControllers:

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

Then replace this:

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

with this:

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

This is so you can add more than one UIViewController to your TabBar Controller.

Then of course, release:

[viewController release];
[viewController1 release];

And under each class for your ViewControllers, under the -initWithNibName method add this inside the if (self) statement:

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

Run your application and it should be working now!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜