我的标签栏中的标签似乎没有显示出来。这是我的App Delegate代码:
(应用程序名称)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];
(编辑:删除上面的第2行和第3行修复了问题,但我需要它们)。有什么想法吗?
答案 0 :(得分: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;
为所有ViewControllers添加额外的alloc和inits:
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];
这样您就可以向TabBar控制器添加多个UIViewController。
当然,发布:
[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"];
运行你的应用程序,它现在应该正常工作!