我有一个nsarray,当我从我的一个方法NSLog它我可以看到它的内容,但当我试图从其他地方看它内部它似乎是空的。我知道我的记忆管理需要一些工作,任何人都可以帮助解释这里发生的事情吗?
新代码
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 (除了设置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];
}
答案 0 :(得分:3)
你的内存管理很好,虽然有点尴尬 - 你可以声明一个保留属性并避免底部的retain
。你的问题来自于'viewWillAppear:'在'passWorkoutBack:'之前被调用的事实,所以它在2处是空白的,因为2在1之前被调用。如果你期望某事在2,那么你的'fetchWorkoutList'方法不是工作正常。
答案 1 :(得分:2)
你的setter方法应该释放前一个值并保留新值,例如
– (void)passWorkoutBack:(NSArray *)workout {
if (passedWorkout != workout) {
[passedWorkout release];
passedWorkout = [workout retain];
}
}
此处,if
语句可确保您在保留之前不会意外释放workout
。
最后,由于您在alloc
中致电viewDidLoad
,您应该在viewDidUnload中调用release
。
由于您在alloc
中呼叫init
和viewDidLoad
,因此调用此方法时,passedWorkout
可能具有的任何值都将被删除。如果您希望保留其值,即使视图已卸载并重新加载,那么您应该在ViewController的passedWorkout
方法中分配init
,当然,也可以在dealloc
中释放它方法。
答案 2 :(得分:0)
我认为这是您对应用流程的理解:
说实话,我认为这是因为缺乏对设计健壮代码的理解(不用担心,这是编程中最难学的东西)。以下是一些我认为会使这款应用流程更顺畅的提示:
Anywho,我希望这有所帮助,而且我不太'preachy'。我假设你的应用程序和你很多,所以纠正我的错误。欢呼声。