简体   繁体   English

iOS(iPhone / iPad)SDK-标签栏中的标签未显示

[英]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: (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: (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 = [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

I believe the problem is in application didFinishLaunchingWithOptions: as the only recent changes I have made to App Delegate have been these lines: 我相信问题出在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];

(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). 创建其他ViewController的属性,并将其合成到实现文件(.m)中。 Also import them in your AppDelegate.m file. 还将它们导入到您的AppDelegate.m文件中。 Here is what your header (.h) file will look like (rootController is your TabBarController): 这是您的标头(.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

And the first few lines of your implementation (.m) file: 以及实现(.m)文件的前几行:

#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: 为您的所有ViewController添加其他分配和初始化:

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. 这样一来,您可以将多个UIViewController添加到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: 在ViewControllers的每个类下,在-initWithNibName方法下,在if (self)语句中添加以下内容:

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! 运行您的应用程序,它现在应该可以运行了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM