2010-05-28 85 views
0

我遇到問題。 我嘗試初始化在xib文件內創建的classViewController中的變量。 我嘗試使用下面的代碼,但是當我追加一個對象到數組時,這些數組並沒有被初始化。 你能幫我嗎? 感謝和抱歉我的英語。使用nibFIle進行初始化失敗

#import <UIKit/UIKit.h> 

@interface AccelerometroViewController : UIViewController <UIAccelerometerDelegate, UITextFieldDelegate, UIAlertViewDelegate>{ 

    //.....  

    NSMutableArray *arrayPosizioni; 
    NSMutableArray *arrayPosizioniCorrenti; 

    NSString *nomePosizioneCorrente; 

} 

-(IBAction)salvaPosizione; 


//... 
@property (nonatomic, assign) NSMutableArray  *arrayPosizioni; 
@property (nonatomic, assign) NSMutableArray  *arrayPosizioniCorrenti; 

@property (nonatomic, assign) NSString  *nomePosizioneCorrente; 

@end 



#import "AccelerometroViewController.h" 
#import "Position.h" 

@implementation AccelerometroViewController 



float actualX; 
float actualY; 
float actualZ; 


@synthesize arrayPosition; 
@synthesize arrayCurrentPosition; 

@synthesize nameCurrentPosition; 

    -(id)init { 
     self = [super init]; 
     if (self != nil) { 
      arrayPosition = [[NSMutableArray alloc]init]; 
      arrayCurrentPosition = [[NSMutableArray alloc]init]; 
      nameCurrentPosition = [NSString stringWithFormat:@"noPosition"]; 
      actualX = 0; 
      actualY = 0; 
      actualZ = 0; 
     } 
     return self; 
    } 


    -(void)updateTextView:(NSString*)nomePosizione 
    { 
     NSString *string = [NSString stringWithFormat:@"%@", nameCurrentPosition]; 
     textEvent.text = [textEvent.text  stringByAppendingString:@"\n"]; 
     textEvent.text = [textEvent.text  stringByAppendingString:string]; 
    } 


    -(IBAction)savePosition{ 

     Posizione *newPosition; 
     newPosition = [[Position alloc]init]; 

     if([newPosition  setValue:(NSString*)fieldNomePosizione.text:(float)actualX:(float)actualY:(float)actualZ]){ 
     //setValue is a method of Position. I'm sure that this method is correct 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Salvataggio Posizione" message:@"Posizione salvata con successo" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil]; 
      [alert show]; 
      [alert release];   
      [arrayPosition addObject:newPosition]; 
     } 
     else{ 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Salvataggio osizione" message:@"Errore nel salvataggio" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil]; 
      [alert show]; 
      [alert release]; 
     } 
    } 

- (void) initialise { 
    arrayPosition = [[NSMutableArray alloc] init]; 
    arrayCurrentPosition = [[NSMutableArray alloc] init]; 
    nameCurrentPosition = @"noPosition"; 
    actualX = 0; 
    actualY = 0; 
    actualZ = 0; 
} 

- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle { 
    if (self = [super initWithNibName:nibName bundle:bundle]) { 
     [self initialise]; 
    } 
} 
+0

在你的頭文件中,變量名是意大利人(我認爲),並在實現文件中使用英文名字。 – 2010-05-28 22:54:00

回答

0

除了Felix對錯誤命名的評論,您的屬性也是錯誤的。對象需要保留或複製,而不是分配(大多數情況下),以確保它們的價值不會消失。你需要擁有它們的所有權。因此我會用以下內容:

@property (nonatomic, readwrite, retain) NSMutableArray *arrayPosition; 
@property (nonatomic, readwrite, retain) NSMutableArray *arrayCurrentPosition; 
@property (nonatomic, readwrite, copy) NSString *nameCurrentPosition; 

然後,因爲你保留的東西你負責釋放它。因此你需要一個dealloc方法來做到這一點。

-(void)dealloc { 
    self.arrayPosition = nil; 
    self.arrayCurrentPosition = nil; 
    self.nameCurrentPosition = nil; 
    [super dealloc]; 
}