iphone - 无法写入 .plist

标签 iphone objective-c ios xcode ipad

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

这是代码

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 天,但没有找到任何东西!

最佳答案

我不太明白你的问题,但我注意到你没有在 setHP: 操作中初始化 viewCont

尝试添加这一行:

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

就在

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

关于iphone - 无法写入 .plist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9955056/

相关文章:

iphone - 如何在 ios 中使用自定义形状创建 uiscrollview?

iphone - 用手指拖动按钮

ios - 单元测试 iOS 中的子模块静态函数? ( swift 4 - Xcode)

ios - 使用 NSNumberFormatter 的字符串复数器

iphone - 如何从EAGLView获取UIImage?

objective-c - 如何正确处理Cocoa应用程序终止?

iphone - 无法创建持久存储协调器

objective-c - iphone 为 iphone 3gs 和 iphone4 构建应用程序

ios - 如何以编程方式使用自动布局将事件指示器定位到其 super View 的中心?

iphone - 在更新数据源和使用 insertRowsAtIndexPaths 后重新加载 tableview 时,UITableView 复制行?