1

So I have to admit, that I had this code working perfectly up until a day or so ago when I added a new view, so I'm rather frustrated at the moment.

The setup: I have a storyboarded app that contains a tabbar. In the AppDelegate, I have the following for attaching the CoreData

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    UINavigationController *navigationController = [[tabBarController viewControllers] objectAtIndex:0];
    GamesViewController *controller = [[navigationController viewControllers] objectAtIndex:0];
    controller.managedObjectContext = self.managedObjectContext;

    return YES;
}

Also in the AppDelegate, I had this method that would set the standard tab background to an image of my choosing:

- (void)customizeInterface {    
    UIImage* tabBarBackground = [UIImage imageNamed:@"tab_background"];
    [[UITabBar appearance] setBackgroundImage:tabBarBackground];
}

So this all worked fine until I added another login view that precedes my tabs. I had to change what the CoreData was initially being set too (from my tabs to my login/initializing view). Below is the new setup of how the storyboard looks.

Here's the new setup in my storyboard

Now when the app loads up... the background image appears initially as before, but only on the first tab. Once I click off if it, it switches to the default gradient color again. If I go back to the first / initial tab, the background does not-reapply itself, it stays as the colored gradient.

Here is the amended applicationDidFinishLaunching code to go along with it:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //instantiate local context
    NSManagedObjectContext *context = [self managedObjectContext];
    if (!context) {
        // Handle the error.
        NSLog(@"Error: Context is null");
    }

    LoginViewController *rootViewController = [LoginViewController alloc];
    rootViewController.managedObjectContext = context;

    return YES;
}

So then what I tried to do, is go into the viewDidLoad in the first VC that my tabbar loads up (GameViewController) and tried adding this to fix the problem:

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:@"dock_background"]];
}

That didn't work, so I also tried using the same original code that I had in my AppDelegate and that also didn't work:

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImage* tabBarBackground = [UIImage imageNamed:@"tab_background"];
    [[UITabBar appearance] setBackgroundImage:tabBarBackground];
}

So... I'm sort of stuck. I've got to be doing (or not doing) something so obvious... Anyone out there have any tips / pointers?

Thanks a ton - Drew

4

1 回答 1

0

Delegate.h file

@interface AppDelegate : UIResponder

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UIImageView *imgV;
@property (strong, nonatomic) UIViewController *viewController;
@property (strong, nonatomic) UITabBarController *tabBarController;

@end

Delegate.m file

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

        self.viewController = [[DeskboardVctr alloc] initWithNibName:@"DeskboardVctr" bundle:nil];




    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.delegate=self;
    self.imgV=[[UIImageView alloc] init];
    self.imgV.frame=CGRectMake(0, 0, 1024, 49);
    [[self.tabBarController tabBar] insertSubview:self.imgV atIndex:1];
    self.tabBarController.delegate=self;
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

    return YES;
}

Tabbar Delegate Methods

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    NSUInteger index=[[tabBarController viewControllers] indexOfObject:viewController];
        switch (index) {
            case 0:
                self.imgV.image=[UIImage imageNamed:@"t1.png"];
                break;
            case 1:
                self.imgV.image=[UIImage imageNamed:@"t2.png"];
                break;
            case 2:
                self.imgV.image=[UIImage imageNamed:@"t3.png"];
                break;
            case 3:
                self.imgV.image=[UIImage imageNamed:@"t4.png"];
                break;
            case 4:
                self.imgV.image=[UIImage imageNamed:@"t5.png"];
                break;
            default:
                break;
        }
    return YES;
}

Hope you may help this thanks

于 2012-08-01T04:38:39.133 回答