【问题标题】:How to access NSURLResponse in different class如何在不同的类中访问 NSURLResponse
【发布时间】:2011-10-04 21:28:32
【问题描述】:

我有以下 zipfile 类,它执行我的 zip 文件的下载。我有一个与我的 zip 文件关联的文件名。我需要知道要传递给 viewcontroller 类的文件名。每当我尝试从不同的类中访问值时,它总是返回 null。我实际上想知道如何使用 ZipFile 中的 responseString 并在 ViewController 中使用它。

/ZipFile.m/

-(NSString *) downloadZipFile{


fileManager = [NSFileManager defaultManager];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
directoryPath = [paths objectAtIndex:0];
filePath = [NSString stringWithFormat:@"%@/ZipFiles.zip", directoryPath];

NSURL *url=[NSURL URLWithString:@"http://www.abc.com/id=123"];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

NSError *error1;
urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error1];

[fileManager createFileAtPath:filePath contents:urlData attributes:nil];

[SSZipArchive unzipFileAtPath:filePath toDestination:directoryPath];
NSLog(@"Finished unzipping database");

Tab_Table_WinAppDelegate *appDelegate = [[[Tab_Table_WinAppDelegate alloc] init] autorelease];
[appDelegate loadingViewControllerDidFinish];

NSString *total = [self total]; // returns value here
return total;
}


- (NSString *)total {


NSString *responseString = [[NSString alloc] initWithData:urlData encoding:NSASCIIStringEncoding];
NSString *fileName = [response suggestedFilename];
NSLog(@"Value : %@", fileName);

return fileName;
   }

/*ViewController.m*/

- (CGSize)imageSizeAtIndex:(NSUInteger)index {
CGSize size = CGSizeZero;
if (index < [self imageCount]) {

    ZipFile *map = [[ZipFile alloc]init];
    size.width = [[map total] floatValue]; // returns null here

    [map release];
}
return size;
 }

【问题讨论】:

    标签: iphone objective-c nsurlconnection nsurl nsurlrequest


    【解决方案1】:
        ZipFile *map = [[ZipFile alloc]init];
    

    这会创建一个新的 ZipFile,这可能不是您想要做的。您需要某种方式将您真正感兴趣的 ZipFile 对象传递给控制器​​。

    【讨论】:

    • 谢谢。但我没有清楚明白你的意思?如何将 ZipFile 对象传递给我的控制器?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多