zp26
zp26

Reputation: 1527

Initialization problem

I have a problem. This is my code. When i try to use the IBAction SavePosition the "arrayPosition" isn't update. Else if i initialize the "arrayPosition" in "SavePosition" the value is stored in the array. Why this anomaly?

#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];
        }
   }

Upvotes: 0

Views: 202

Answers (2)

deanWombourne
deanWombourne

Reputation: 38475

Whats going on?

I bet you're creating your view controller inside a xib file?

If you set a breakpoint inside your init method on the line

arrayPosition = [[NSMutableArray alloc]init];

I bet it never runs. This means that when you get to the line

[arrayPosition  addObject:newPosition];

arrayPosition is still nil so nothing happens.

How to fix it?

If you're initializing a UIViewController it's either called inside initWithNibName:bundle: if you've created it in code or in initWithCoder: if it's created inside a xib file.

You need to do something like this :

- (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];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)decoder {
    if (self = [super initWithCoder:decoder]) {
        [self initialise];
    }
    return self;
}

This will call initailise regardless of how the view controller is created.

Upvotes: 4

TechZen
TechZen

Reputation: 64428

This:

[newPosition setValue:(NSString*)fieldNomePosizione.text:(float)actualX:(float)actualY:(float)actualZ]

... is gibberish. I don't even see how it compiles. I assume its a typo. However, if the function does not return a boolean it will return a void pointer and always evaluate to false so the block is never called.

More generally, your problem is most likely that you are not using the self.propertyName notation to refer to your properties so they are not being retained. E.g.

[arrayPosition  addObject:newPosition];

should be:

[self.arrayPosition  addObject:newPosition];

... as should all other references to arrayPosition in the code.

Upvotes: 0

Related Questions