繁体   English   中英

文件的路径由另一个视图控制器修改

[英]Path to file is modified by another view controller

我有2个将请求发送到Web服务的视图控制器。 接收到数据后,数据将保存到“文档”文件夹中的文件中。

这些是2个VC:

Live_VC:

#import "FV_Live_ViewController.h"

@interface FV_Live_ViewController ()
@end

@implementation FV_Live_ViewController

NSArray  *paths;
NSString *documentsDirectory;
NSString *path;

- (void)viewDidLoad {

paths              = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths firstObject];

[self sendRequest];    // first request of data ("Live" data)
}

- (void)sendRequest {
// other code

// request of "Live" data
[urlRequest startWithCompletion:^(URLRequest *request, NSData *data, NSError *error, BOOL success) {
    if (success) {
        // other code

        NSString *filename = [NSString stringWithFormat:@"months.plist"];   
        path = [documentsDirectory stringByAppendingPathComponent:filename];    // path = "...\months.plist"

        // second request of data (monthly data) if months.plist doesn't exists
        if (![[NSFileManager defaultManager] fileExistsAtPath: path]) {
            [self sendMonthRequest];
        }
    }
}];
}    

- (void)sendMonthRequest {

[urlRequest startWithCompletion:^(URLRequest *request, NSData *data, NSError *error, BOOL success) {            
    if (success) {
        // other code

        [monthlyArray writeToFile: path atomically:YES];    // path should be "...\months.plist" while it is "...\yesterday.plist"
    }
}];
}

@end

Today_VC:

#import "FV_Today_ViewController.h"

@interface FV_Today_ViewController ()
@end

@implementation FV_Today_ViewController

NSArray  *paths;
NSString *documentsDirectory;
NSString *path;

- (void)viewDidLoad {

paths              = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths firstObject];

[self sendRequest];        // first call of "sendRequest" to get today data
}

- (void)sendRequest {
//  other code

// request of "Today" data
[urlRequest startWithCompletion:^(URLRequest *request, NSData *data, NSError *error, BOOL success) {
    if (success) {       
        //  other code

        NSString *filename = [NSString stringWithFormat:@"today.plist"];
        path = [documentsDirectory stringByAppendingPathComponent:filename];    // path = "...\today.plist"

        [dataDictionary writeToFile: path atomically:YES];      // save data to today.plist

        // second call of "sendRequest" to get yesterday data only if yesterday.plist doesn't already exists)           
        filename = [NSString stringWithFormat:@"yesterday.plist"];
        path = [documentsDirectory stringByAppendingPathComponent:filename];    // path = "...\yesterday.plist"
        if (![[NSFileManager defaultManager] fileExistsAtPath:path])
            [self sendRequest];
    }
}];
}

@end

在每个VC中,我都使用NSString(“路径”)存储路径,但是问题在于,在“ sendMonthRequest”方法(Live_VC)中,路径的值是在另一个VC(Today_VC)中设置的值。 怎么可能呢? 第二个VC如何更改第一个VC中的NSString的值?

谢谢,Corrado

之所以得到此结果,是因为您将“路径”设置为文件顶部,从而使其成为全局变量。 用花括号将其包围,然后它将是一个普通的ivar,

@implementation FV_Today_ViewController {

NSArray  *paths;
NSString *documentsDirectory;
NSString *path;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM