简体   繁体   English

我可以在iOS7的其他ViewController中使用NSURLSession委托方法吗?

[英]Can I have a NSURLSession delegate method in a different ViewController in iOS7?

I am using iOS7 and Xcode 5. 我正在使用iOS7和Xcode 5。

I have instantiated a NSURLSession object with a URL in one ViewController and then I want to write its delegate in a different ViewController. 我在一个ViewController中实例化了一个带有URL的NSURLSession对象,然后我想在另一个ViewController中编写其委托。 Is it possible? 可能吗?

Basically, I want to make the call in one ViewController, and immediately after making the call I transit to the other ViewController. 基本上,我想在一个ViewController中进行调用,并且在调用之后立即转到另一个ViewController。 Once the transition is made, I want to receive the response in the second ViewController through the NSURLSession delegate methods and then display the data. 转换完成后,我想通过NSURLSession委托方法在第二个ViewController中接收响应,然后显示数据。 Is this possible? 这可能吗?

Note: I don't want to keep the user waiting in the first ViewController until the server comes back with its response, because while the server is processing the data, I can keep the user engaged in the second ViewController with other images/elements displayed. 注意:在服务器返回响应之前,我不想让用户在第一个ViewController中等待,因为在服务器处理数据时,我可以让用户在第二个ViewController中与其他图像/元素一起显示。

Your Api.h 您的Api.h

#import <Foundation/Foundation.h>
typedef void(^successTypeBlock)(NSData*);
typedef void(^errorTypeBlock)(NSError*);
@interface TestApi : NSObject<NSURLConnectionDelegate>

-(void)hitApiWithURL:(NSURL*)url successBlock:(successTypeBlock)success  failureBlock:(errorTypeBlock)failure;
@end

your Api.m 您的Api.m

@interface  TestApi()
{

}
@property(nonatomic,copy) errorTypeBlock    errorBlock_;
@property(nonatomic,copy) successTypeBlock  successBlock_;
@end
@implementation TestApi


-(void)hitApiWithURL:(NSURL*)url successBlock:(successTypeBlock)success  failureBlock:(errorTypeBlock)failure{
    errorBlock_=failure;
    successBlock_=success;
    NSURLRequest   *request=[NSURLRequest requestWithURL:url];
    [NSURLConnection connectionWithRequest:request delegate:self];

}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{

}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{
    successBlock_(data);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{



}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    errorBlock_(error);
}



@end

Now 现在

[<yourApiObj>   hitApiWithURL:<your URL> successBlock:^(NSData* data){
//get success value here after server responce

}  failureBlock:^(NSError* error){

//get error if any
}];

just gave you a basic skeleton improve and innovate as per your needs. 只是为您提供了根据您的需求进行改进和创新的基本框架。

Rather than doing it on the main thread I think you need use threading concept so that, you can accomplish this using the secondary thread. 我认为您无需在主线程上执行此操作,而是需要使用线程概念,以便可以使用辅助线程来完成此操作。 And even the UI will not get blocked. 甚至UI也不会被阻止。

Write a class and subclass it with NSOperation. 编写一个类,并使用NSOperation对其进行子类化。

refer this 参考这个

  1. Apple's Sample code : https://developer.apple.com/library/mac/samplecode/NSOperationSample/Introduction/Intro.html#//apple_ref/doc/uid/DTS10004184 苹果的示例代码: https : //developer.apple.com/library/mac/samplecode/NSOperationSample/Introduction/Intro.html#//apple_ref/doc/uid/DTS10004184

  2. Refer this wonderfull tutorial. 请参阅此精彩教程。 http://www.raywenderlich.com/19788/how-to-use-nsoperations-and-nsoperationqueues http://www.raywenderlich.com/19788/how-to-use-nsoperations-and-nsoperationqueues

Hope this will help you. 希望这会帮助你。

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

相关问题 如何在ViewController中获取委托方法以查看*记录器何时完成(不同类) - How can I get a delegate method in ViewController to see when *recorder is finished (different class) iOS - 从 ViewController 调用 App Delegate 方法 - iOS - Calling App Delegate method from ViewController 自从iOS7中的delete(backspace)操作以来,我如何不接收UITextView的委托消息? - How can I not to receive UITextView's delegate message since delete(backspace) action in iOS7? 将参数附加到 NSURLSession 委托方法 - Attach parameters to NSURLSession Delegate Method iOS7 viewController和MPMoviePlayerViewController旋转 - iOS7 viewController and MPMoviePlayerViewController Rotation 我应该如何在backgroundConfiguration中使用NSURLSession将多个文件的块上传到Amazon S3(iOS7) - How should I use NSURLSession in backgroundConfiguration to upload chunks of multiple files to Amazon S3 ( iOS7 ) 将进度从NSURLSession发送到ViewController [swift-iOS] - Send Progress from NSURLSession to ViewController [swift - iOS] IOS:如何直接在方法的参数中覆盖委托? - IOS : How can I override a delegate directly in the parameters of a method? UILabel 在 iOS7 中有一个奇怪的灰色顶线/边框,我该如何删除它? - UILabel have a weird grey top line/border in iOS7, how can I remove it? iOS7中的prepareForSegue方法 - prepareForSegue method in iOS7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM