2012-03-31 94 views
1

我有一个默认情况下将youtube作为主页的小应用程序。您可以在一个小菜单中更改它,但它不起作用,我不知道为什么。无法在.plist中写入

下面的代码

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 

因此,任何人知道我在做什么错?请帮助我,我在网上搜索了4天,但没有找到任何东西!

+0

紫云,你需要很多更具体的了解您的问题:哪里该走了?你究竟想达到什么目的? .plist文件是否正确保存但未加载?内容被读错了吗?帮助我们帮助你。 – Stavash 2012-03-31 11:00:03

+0

我想要实现的是更改主页,因为它不起作用。 – 2012-03-31 11:42:22

回答

1

我不明白你的问题,但我注意到你没有初始化viewContsetHP:行动。

尝试加入这一行:

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

只是

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

哇!它现在工作正常!太谢谢你了! – 2012-03-31 11:43:41