2009-11-16 4 views
1

J'ajoute par programme un UIImageView et un UILabel à plusieurs écrans dans un programme sur lequel je travaille. En tant que tel, j'ai écrit un UIViewController qui contient le code pour charger le UIImageView et UILabel en viewDidLoad, avec une minuterie pour mettre à jour le contenu. J'utilise ce contrôleur de vue comme une superclasse pour toutes les vues qui ont besoin de UIImageView et UILabel.UIImageView dérivant sur viewDidLoad

Pour la plupart, cela fonctionne très bien, mais lors de la transition d'écrans spécifiques, la paire semble commencer avec une trame de (0,0,0,0) et animer lentement à l'image que j'ai définie. Je n'ai aucune idée de ce qu'est le problème ou même où regarder. Toute indication ici serait géniale.

Voici le code que j'utilise comme ma superclasse.

FoundationViewController.h

#import 

@interface FoundationViewController : UIViewController { 
    IBOutlet UIImageView *activeInventoryImage; 
    IBOutlet UILabel *activeInventoryLabel; 
    NSTimer *inventoryStatusTimer; 
} 

@property (nonatomic, retain) IBOutlet UIImageView *activeInventoryImage; 
@property (nonatomic, retain) IBOutlet UILabel *activeInventoryLabel; 

- (void) advanceToView:(id)nextView; 
- (void) inventoryStatus; 
- (BOOL) inventoryActive:(NSDate*)inventoryExpire withImage:(NSString*)imageName; 
@end 

FoundationViewController.m

#import "FoundationViewController.h" 
#import "GameData.h" 
#import "Globals.h" 


@implementation FoundationViewController 

@synthesize activeInventoryImage; 
@synthesize activeInventoryLabel; 

- (void) viewDidLoad{ 

    // initialize the active inventory icon and label 
    activeInventoryImage = [[UIImageView alloc] init]; 
    activeInventoryLabel = [[UILabel alloc] init]; 

    // clear 
    activeInventoryImage.image = [UIImage imageNamed:@""]; 
    activeInventoryLabel.text = @""; 

    // set the center points 
    activeInventoryImage.frame = CGRectMake(258, 7, 20, 20); 
    activeInventoryLabel.frame = CGRectMake(280, 6, 30, 20); 

    // set label parameters 
    activeInventoryLabel.backgroundColor = [UIColor clearColor]; 
    activeInventoryLabel.font = [UIFont fontWithName:@"Helvetica" size:11.0f]; 
    activeInventoryLabel.textColor = [UIColor colorWithRed:0.2 green:0.4 blue:0.6 alpha:1]; 

    // add to view 
    [self.view addSubview:activeInventoryImage]; 
    [self.view addSubview:activeInventoryLabel]; 

    // start the timer if there is any active inventory 
    if(
     [self inventoryActive:[GameData sharedGameData].player.inventory.aExpire withImage:@"a.png"] || 
     [self inventoryActive:[GameData sharedGameData].player.inventory.bExpire withImage:@"b.png"] || 
     [self inventoryActive:[GameData sharedGameData].player.inventory.cExpire withImage:@"c.png"] 
     ) 
     inventoryStatusTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(inventoryStatus) userInfo:nil repeats:YES]; 

    [super viewDidLoad]; 
} 

- (BOOL) inventoryActive:(NSDate*)inventoryExpire withImage:(NSString*)imageName{ 
    NSTimeInterval expireSeconds; 
    NSDateFormatter *formatter; 
    BOOL runTimer = FALSE; 

    // expire 
    expireSeconds = [inventoryExpire timeIntervalSinceNow]; 
    if(expireSeconds > 0){ 
     formatter = [[NSDateFormatter alloc] init]; 
     [formatter setDateFormat:@"mm:ss"]; 
     NSDate *expireTime = [NSDate dateWithTimeIntervalSince1970:expireSeconds]; 
     activeInventoryImage.image = [UIImage imageNamed:imageName]; 
     activeInventoryLabel.text = [formatter stringFromDate:expireTime]; 
     [formatter release]; 
     runTimer = TRUE; 
    } 

    return runTimer; 
} 

- (void) inventoryStatus{ 

    // if there is no active inventory kill the timer 
    if(
     ![self inventoryActive:[GameData sharedGameData].player.inventory.aExpire withImage:@"a.png"] && 
     ![self inventoryActive:[GameData sharedGameData].player.inventory.bExpire withImage:@"b.png"] && 
     ![self inventoryActive:[GameData sharedGameData].player.inventory.cExpire withImage:@"c.png"] 
     ){ 
     activeInventoryImage.image = [UIImage imageNamed:@""]; 
     activeInventoryLabel.text = @""; 
     if(inventoryStatusTimer != nil) 
      [inventoryStatusTimer invalidate]; 
    } 

} 

- (void) advanceToView:(id)nextView{ 
    if([nextView isKindOfClass:[UIView class]]){ 
     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDuration:2.7]; 
     [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.view cache:YES]; 
     [self.view addSubview:nextView]; 
     [UIView commitAnimations]; 
    } 
    else 
     NSLog(@" Error Loading View: %@",nextView); 
} 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
    [activeInventoryImage removeFromSuperview]; 
    [activeInventoryImage release]; 
    [activeInventoryLabel release]; 
    [super dealloc]; 
} 


@end 

Répondre

0

Après quelques expériences j'ai découvert en utilisant initWithFrame corrige le problème de dérive.

activeInventoryImage = [[UIImageView alloc] initWithFrame:CGRectMake(258, 7, 20, 20)]; 
activeInventoryLabel = [[UILabel alloc] initWithFrame:CGRectMake(280, 6, 30, 20)]; 

Je n'ai toujours aucune idée de la cause du problème. Si quelqu'un pouvait me dire ce qu'il se passe ici, j'aimerais savoir pour référence future.

1

Mettre un point de rupture dans advanceToView comme cela semble être votre seul code d'animation. Chaque fois qu'il est appelé, regardez la pile d'appels dans le débogueur. De là, vous pouvez voir ce qui appelle ce code quand ils ne sont pas supposés l'être.

+0

J'ai laissé tomber un point de rupture dans advanceToView et vérifié qu'il est appelé comme prévu (seulement une fois sur les changements d'écran initiés par les boutons poussés). J'ai également commenté tout le code d'animation dans la routine et laissé seul [self.vew addSubView: nextView]; Je vois toujours le comportement sur un petit sous-ensemble de mes viewControllers. J'ai remarqué que l'animation n'est pas présente pendant que je traverse le débogueur. C'est, cependant, quand je cours sans points de rupture. Je suis toujours perplexe. Des indicateurs ici? –