2012-03-31 12 views
1

Ich habe eine kleine App, die standardmäßig Youtube als Startseite haben. Sie können es in einem kleinen Menü ändern, aber es funktioniert nicht und ich weiß nicht warum.Kann nicht schreiben in .plist

Hier ist der Code

viewController 

.h 
#import <UIKit/UIKit.h> 


@interface ViewController : UIViewController{ 
    IBOutlet UIWebView *webView; 

} 
@property (retain, nonatomic) NSArray *array; 
@property (retain, nonatomic) NSString *string1; 
-(NSString *) dataFilePath; 
- (IBAction)settings:(id)sender; 
- (IBAction)home:(id)sender; 

@end 

.m 

#import "ViewController.h" 
#import "settings.h" 


@implementation ViewController 
@synthesize array; 
@synthesize string1; 
- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

    if ([string1 isEqual:@"youtube"]) { 
NSMutableArray *Array = [[NSMutableArray alloc] init]; 
[Array addObject:@"http://www.youtube.com"]; 
[Array writeToFile:[self dataFilePath] atomically:YES]; 

    } 

NSString *filePath = [self dataFilePath]; 
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) 
{ 
    array = [[NSArray alloc] initWithContentsOfFile:filePath]; 
} 
NSString *string = [NSString stringWithString:[array objectAtIndex:0]]; 
    [webView loadRequest:[[NSURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:string]]]; 

}     

-(NSString *) dataFilePath 
{ 
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentDirectory = [path objectAtIndex:0]; 

    return [documentDirectory stringByAppendingPathComponent:@"url.plist"]; 
    string1 = @"youtube"; 

} 

- (IBAction)settings:(id)sender { 
    settings *NView = [[settings alloc] initWithNibName:nil bundle:nil]; 
    [self presentModalViewController:NView animated:YES]; 

} 

- (IBAction)home:(id)sender { 
    NSString *filePath = [self dataFilePath]; 
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) 
    { 
     array = [[NSArray alloc] initWithContentsOfFile:filePath]; 
    } 
    NSString *string = [NSString stringWithString:[array objectAtIndex:0]]; 
    NSLog(@"%@\n",string); 
    [webView loadRequest:[[NSURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:string]]]; 
} 
- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

@end 

Settings //here's the menu where I can change the homepage 

.h 

#import <UIKit/UIKit.h> 
#import "ViewController.h" 

@interface settings : UIViewController{ 
    ViewController *viewCont; 
    IBOutlet UITextField *field; 

} 
-(IBAction)back:(id)sender; 
- (IBAction)setHP:(id)sender; 

@end 

.m 


#import "settings.h" 
#import "ViewController.h" 


@implementation settings 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib 

} 

- (void)viewDidUnload 
{ 
    field = nil; 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 
-(IBAction)back:(id)sender{ 
    [self dismissModalViewControllerAnimated:YES]; 
} 

- (IBAction)setHP:(id)sender { 

    NSMutableArray *Array = [[NSMutableArray alloc] init]; 
    [Array addObject:field.text]; 
    [Array writeToFile:[viewCont dataFilePath] atomically:YES]; 
    [viewCont.string1 initWithString:@"other" ]; 
    NSLog(@"%@\n", viewCont.string1);// HERE XCODE SAYS string1 = null!!! WHY? 

} 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

@end 

So jemand weiß, was mache ich falsch? Bitte hilf mir, ich habe 4 Tage im Internet gesucht und nichts gefunden!

+0

Jaun, Sie brauchen viel mehr spezifisch über Ihr Problem sein: Wo dies schief geht? Was genau willst du erreichen? Ist die .plist-Datei korrekt gespeichert, aber nicht geladen? Werden die Inhalte falsch gelesen? Hilf uns, dir zu helfen. – Stavash

+0

Was ich versuche zu erreichen, ist die Homepage zu ändern, weil es nicht funktioniert. –

Antwort

1

Ich verstehe nicht gut Ihr Problem, aber ich merke, dass Sie viewCont in der setHP: Aktion nicht initialisieren.

versuchen, diese Zeile hinzufügen:

ViewController *viewCont=[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 

kurz vor

[viewCont.string1 initWithString:@"other" ]; 
+0

Wow! Es funktioniert jetzt gut! Vielen Dank Mat! –