днем все,
Я попытался добавить второй объект данных в постоянное хранилище в учебном коде (местоположения) coredata, а затем получить к нему доступ в новом представлении. Я думаю, что я следовал этому руководству и проверил, что я делаю чистую сборку и т. Д., Но не вижу, что изменить, чтобы предотвратить его сбой.
Боюсь, я в этом сошел с ума и, похоже, не могу найти тот шаг, который я пропустил. Я вставил файлы заголовков и кодов ниже, пожалуйста, дайте мне знать, если мне нужно больше поделиться кодом. Похоже, авария произошла на линии:
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Album" inManagedObjectContext:[self managedObjectContext]];
В коде есть еще одна строка, которая в данный момент ссылается на galleryviewcontroller, и она находится в основном делегате приложения:
galleryViewController.managedObjectContext = [self managedObjectContext];
GalleryViewController.h
#import <UIKit/UIKit.h>
@interface GalleryViewController : UIViewController {
NSManagedObjectContext *managedObjectContext;
int rowNumber;
IBOutlet UILabel *lblMessage;
UIBarButtonItem *addButton;
NSMutableArray *imagesArray;
}
@property (readwrite) int rowNumber;
@property (nonatomic,retain) UILabel *lblMessage;
@property (nonatomic,retain) NSMutableArray *imagesArray;
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) UIBarButtonItem *addButton;
-(void)updateRowNumber:(int)theIndex;
-(void)addImage;
@end
GalleryViewController.m
#import "RootViewController.h"
#import "LocationsAppDelegate.h"
#import "Album.h"
#import "GalleryViewController.h"
#import "Image.h"
@implementation GalleryViewController
@synthesize lblMessage,rowNumber,addButton,managedObjectContext;
@synthesize imagesArray;
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/
-(void)updateRowNumber:(int)theIndex{
rowNumber=theIndex;
LocationsAppDelegate *mainDelegate =(LocationsAppDelegate *)[[UIApplication sharedApplication] delegate];
Album *anAlbum = [mainDelegate.albumsArray objectAtIndex:rowNumber];
lblMessage.text = anAlbum.uniqueAlbumIdentifier;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addImage)];
addButton.enabled = YES;
self.navigationItem.rightBarButtonItem = addButton;
/* Found this in another answer, adding it to the code didn't help.
if (managedObjectContext == nil) {
managedObjectContext = [[[UIApplication sharedApplication] delegate] managedObjectContext];
}
*/
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Album" inManagedObjectContext:[self managedObjectContext]];
[request setEntity:entity];
// Order the albums by creation date, most recent first.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"imagePath" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
[sortDescriptors release];
// Execute the fetch -- create a mutable copy of the result.
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil) {
// Handle the error.
}
[self setImagesArray:mutableFetchResults];
int a = 5;
int b = 10;
for( int i=0; i<[imagesArray count]; i++ )
{
if( a == 325 )
{
a = 5;
b += 70;
}
UIImageView *any = [[UIImageView alloc] initWithFrame:CGRectMake(a,b,70,60)];
any.image = [imagesArray objectAtIndex:i];
any.tag = i;
[self.view addSubview:any];
[any release];
a += 80;
}
}
-(void)addImage{
NSString *msg = [NSString stringWithFormat:@"%i",rowNumber];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Add image to" message:msg
delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
[alert show];
[alert release];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)dealloc {
[lblMessage release];
[managedObjectContext release];
[super dealloc];
}
@end