我有一個問題。 這是我的代碼。 當我嘗試使用IBAction SavePosition「arrayPosition」不更新。 否則,如果我在「SavePosition」中初始化「arrayPosition」,則該值將存儲在數組中。 爲什麼這個異常?初始化問題
#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];
}
}
嗨。你在savePosition中有內存泄漏!您需要在最後釋放newPosition(或在您分配/初始化時自動釋放它) – deanWombourne 2010-05-28 13:16:02