1

このクラスに関連付けられたビューをロードすると、デリゲート メソッドを呼び出すことができないようです。いずれも起動しません。それは私が見落としてきたものだと確信していますが、私の人生では理由を理解することはできません.

DownloadUpdates.h

#import <UIKit/UIKit.h>

@interface DownloadUpdates : UIViewController

    @property (strong, nonatomic) NSMutableData *responseData;
    @property (strong, nonatomic) NSURLConnection *connection;

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

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

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

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

@end

DownloadUpdates.m

この URL はプライバシー保護のために削除されていますが、JSON データを返す API を呼び出しているだけです。この URL は期待どおりに機能するため、コードの問題です。

#import "DownloadUpdates.h"

@interface DownloadUpdates ()

@end

@implementation DownloadUpdates

- (void)connection:(NSURLConnection *)_connection didReceiveResponse:(NSURLResponse *)response
{
    _responseData = [[NSMutableData alloc] init];
    NSLog(@"Response received");
}

- (void)connection:(NSURLConnection *)_connection didReceiveData:(NSData *)data
{
    [_responseData appendData:data];
        NSLog(@"Data received");
}

- (void)connection:(NSURLConnection *)_connection didFailWithError:(NSError *)error
{
    NSLog(@"Unable to fetch data");
}

- (void)connectionDidFinishLoading:(NSURLConnection *)_connection
{
    NSLog(@"Succeeded! Received %d bytes of data",[_responseData
                                                   length]);
//    NSString *txt = [[NSString alloc] initWithData:_responseData encoding: NSASCIIStringEncoding];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *myURL = [NSURL URLWithString:@"URL HERE"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];

    _connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

あなたが提供できる助け/アドバイスをいただければ幸いです。

4

2 に答える 2

2

プロパティを使用して、強力な修飾子を適用します。

self.responsedata = [[NSMutableData alloc] init];
(.....)
self.connection = [[NSURLConnection alloc] initWithRequest: ....etc...
于 2013-06-10T14:46:32.050 に答える
0

NSLogまたはブレークポイントを挿入しviewDidLoadて、これがまったく呼び出されるようにすることをお勧めします。たとえば、DownloadUpdatesストーリーボード シーンのクラスとして指定しなかった場合に、これが発生する可能性があります。

于 2013-06-10T16:58:22.170 に答える