Hallo,
ich bin Noob in Sachen Objective-C und versuche mich gerade druchzuwurschteln und die Sprache und das Handlich zu lernen.
Ich habe ein Projekt, mit einer Appdelegate.h und m sowie einer mainViewController.h und m und xib. Aus der mainViewController wird per Button eine neue View geöffnet. (update.h .m und xib)
update *ud =[[update alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:ud animated:YES];
die update.h sieht folgendermaßen aus:
@interface update : UIViewController
-(IBAction)closeSelf;
@end
die dazugehörige m:
#import "update.h"
#import "dataupdate.h"
@interface update ()
@end
@implementation update
-(void)closeSelf
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:@"update" bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(void)awakeFromNib
{
dataupdate *du=[[dataupdate alloc] init];
[du updatedatas];}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Also nix weltbewegendes. Jedoch wird mir die Methode updatedatas noch vor dem Erscheinen der View ausgeführt, obwohl diese in awakeFromNib ausgeführt wird. Es soll aber anders herum sein. Erst soll die neue View geladen werden und dann die Funktion updatedatas ausgeführt werden.
Die dataupdate sieht folgendermaßen aus:
#import "dataupdate.h"
#import "AppDelegate.h"
@implementation dataupdate
-(void)updatedatas
{
NSError *error;
AppDelegate *appDelegate=[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Entity" inManagedObjectContext:context];
NSFetchRequest *request =[[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
NSArray *items =[context executeFetchRequest:request error:&error];
for(NSManagedObject *managedObject in items)
[context deleteObject:managedObject];
NSString *urlString=[NSString stringWithFormat:@"http://irgendeineURL/und_da_eine_json_Datei"];
NSURL *url =[NSURL URLWithString:urlString];
NSData *data = [NSData dataWithContentsOfURL:url];
NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
for(int x=0;x<json.count;x++)
{
NSManagedObject *newContact;
newContact = [NSEntityDescription insertNewObjectForEntityForName:@"Entity" inManagedObjectContext:context];
[newContact setValue:[NSNumber numberWithDouble:[[[json objectAtIndex:x] valueForKey:@"id"] doubleValue]] forKey:@"EntitybId"];
[newContact setValue:[[json objectAtIndex:x] valueForKey:@"Name"]forKey:@"EntityName"];
[context save:&error];
NSLog([[NSNumber numberWithInt:x] stringValue]);
}
}
@end
Irgendwo habe ich nen Denkfehler. Aber ich weiß noch nicht wo. Könnt Ihr helfen?
Grüße und Danke schonmal
|