【发布时间】:2011-07-07 23:43:28
【问题描述】:
我有一个 nsarray,当我从我的一个方法 (在 WorkOutList.m 中) 对其进行 NSLog 记录时,我可以看到它的内容,但是当我尝试从 WorkOutList 中的不同方法查看它时.m 它似乎是空的。我知道我的内存管理需要一些工作,谁能帮忙解释一下这里发生了什么?
我正在使用 popViewControllerAnimated:YES 将视图从 tableView 返回到视图控制器,但就在我这样做之前,我在 WorkOutList 中的一个方法中设置了我的数组。当我从同一方法 NSLog 那个数组时,我返回了结果,但是当我从其他地方 NSLog 它返回空时。
有人告诉我,它可能是数组初始化的 viewDidLoad 方法,但该方法中的另一个数组 customWorkouts 仍然保留其数据。所以我不知道,任何解释都会很有帮助。我希望它能够工作,但我也很想理解它,这样我才能正确编码。
非常感谢!
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];
}
【问题讨论】:
-
哪里有方法[self fetchWorkoutList];
-
您能否明确命名您遇到问题的阵列?它从哪个方法(名称)起作用,从哪个不起作用?
标签: iphone objective-c ios xcode