开发者

NSArray losing data in objective-c, Could someone explain why?

I have an nsarray that when I NSLog it from one of my methods (inside WorkOutList.m) I can see its contents, but when I try to look inside it from a different method inside WorkOutList.m it seems to be empty. I am aware that my memory management needs some work, could anyone help explain whats happening here?

I am using popViewControllerAnimated:YES to return the view from a tableView back to a view controller, but just before I do that I set my array in a method inside WorkOutList. When I NSLog that array from that same method I am returned results, however when i NSLog it from else where it is returned empty.

I have been told that it might be the viewDidLoad method where the array is init, but that the other array in that method customWorkouts still retains its data. So i dunno, any explanation would be really helpful. I want this to work, but I also really want to understand it so I can get on with coding correctly.

Thanks so much!

WorkOutList.h

#import <UIKit/UIKit.h>


@interface WorkOutList : UIViewController {

    NSManagedObjectContext *managedObjectContext;
  开发者_运维技巧  NSMutableArray *customWorkouts;
    NSArray *passedWorkout;
}

@property(nonatomic, retain)NSManagedObjectContext *managedObjectContext;
@property(nonatomic, retain)NSMutableArray *customWorkouts;
@property(nonatomic, retain)NSArray *passedWorkout;

-(IBAction)customWorkouts:(id)sender;

-(void)passWorkoutBack:(NSArray *)workout;


@end

WorkOutList.m

@implementation WorkOutList

@synthesize managedObjectContext, customWorkouts, passedWorkout;

- (void)viewDidLoad {
    [self setupContext];
    NSLog(@"View Did Load");
    customWorkouts = [[NSMutableArray alloc] init];
    passedWorkout = [[NSArray alloc] init];
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self fetchWorkoutList];
    NSLog(@"View will Appear");
    NSLog(@"Array from View Will Appear : %@", passedWorkout);

}

-(IBAction)customWorkouts:(id)sender{
    CoCoachAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    SelectedWorkout *selectedWorkout = [[SelectedWorkout alloc] initWithStyle:UITableViewStyleGrouped];

    [selectedWorkout recieveNeededData:customWorkouts];

    [appDelegate.practiceNavController pushViewController:selectedWorkout animated:YES];
    [selectedWorkout release];
}

-(void)passWorkoutBack:(NSArray *)workout{
    passedWorkout = workout;
    [passedWorkout retain];
}

- (void)dealloc {
    [super dealloc];
}

SelectedWorkout.h

#import <UIKit/UIKit.h>


@interface SelectedWorkout : UITableViewController {


    NSMutableArray *workoutListForTable;
}

@property(nonatomic,retain)NSMutableArray *workoutListForTable;

-(void)recieveNeededData:(NSMutableArray *)workoutList;

@end

SelectedWorkout.m(aside from all the stuff to set up the tableView)

    @implementation SelectedWorkout

    @synthesize workoutListForTable;

    -(void)recieveNeededData:(NSMutableArray *)workoutList{

        if (workoutListForTable != workoutList) {
            workoutListForTable = workoutList;  
        }

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];

        CoCoachAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
        WorkOutList *workoutListView = [[WorkOutList alloc]init];

        [workoutListView passWorkoutBack:[workoutListForTable objectAtIndex:indexPath.row]];
        [appDelegate.practiceNavController popViewControllerAnimated:YES];
    }

    - (void)dealloc {

        [workoutListForTable release];
        [super dealloc];
    }


    NSLog(@"other table : %@", workoutListForTable);
    [workoutListForTable retain];
}


In this line:

passedWorkout = [[NSArray alloc] init];

You're creating an immutable array with nothing in it. What do you want it to contain?


It looks to me that although you are initializing your array with or so I assume, since on the other line you are only allocating an empty array.

[self fetchWorkoutList]

You are resetting it here every time:

[workoutListView passWorkoutBack:[workoutListForTable objectAtIndex:indexPath.row]];

As a note here:

-(void)passWorkoutBack:(NSArray *)workout{
    passedWorkout = workout;
    [passedWorkout retain];
}

You have the property passedWorkout as retain already, however you need to call it on self

-(void)passWorkoutBack:(NSArray *)workout{
    self.passedWorkout = workout;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜