我有问题。 我尝试在xib文件中创建的classViewController中初始化变量。 我尝试使用下面的代码,但是当我将一个对象附加到数组时,这些数组未初始化。 你能帮助我吗? 谢谢,抱歉我的英语。
#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 :(得分: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];
}