我有一個項目,我正在工作,有許多不同的新聞源和公告板,顯示來自各種來源的帖子。目前,我擁有類似代碼,刪除和標記每個類文件中包含的用於顯示提要的視圖的按鈕。我一直在設計一個實用程序類,允許我將上面列出的三個功能的代碼放置在整個項目中使用的一個對象中。我用C++或Java完成了完全相同的類型,但是在objective-c中有問題。類似的,刪除和標誌按鈕使用NSURL庫與Web服務進行交互。貝婁是我試圖在實用類來實現的一種方法的一個例子,並且是用在像按鈕實現代碼:IOS創建工具類對於NSURL
+ (void)btnLikeAction:(UIButton *)btnLike userIdString:(NSString *)userId contentSource:(NSString *)sourceId cellUsed:(NewsfeedCell *)cell dataForCell:(Post *)post
{
BOOL hasLiked = post.hasLiked;
UILabel *likeLabel = cell.likeCount;
NSString *pId = post.postId;
if (hasLiked)
{
[btnLike setEnabled:NO];
NSString *url = [NSString stringWithFormat: @"http://192.155.94.183/api/v1/likes/unlike/%@/%@/%@/", sourceId, pId, userId];
NSURL *URL = [NSURL URLWithString:url];
NSURLRequest *request = [NSURLRequest requestWithURL:URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
int localCount = [likeLabel.text intValue];
localCount--;
likeLabel.text = [NSString stringWithFormat:@"%i", localCount];
post.likeCount = [NSString stringWithFormat:@"%i", localCount];
post.hasLiked = NO;
[btnLike setEnabled:YES];
}
else
{
[btnLike setEnabled:NO];
NSError *err = nil;
NSDictionary *likeData = [NSDictionary dictionaryWithObjectsAndKeys:
userId, @"user_id",
pId, @"source_id",
sourceId, @"source",
nil];
NSData *JSONLike = [NSJSONSerialization dataWithJSONObject:likeData options:NSJSONWritingPrettyPrinted error:&err];
NSString *url = @"http://192.155.94.183/api/v1/likes.json";
NSURL *URL = [NSURL URLWithString:url];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:30.0];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [JSONLike length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:JSONLike];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
int localCount = [likeLabel.text intValue];
localCount++;
likeLabel.text = [NSString stringWithFormat:@"%i", localCount];
post.likeCount = [NSString stringWithFormat:@"%i", localCount];
post.hasLiked = YES;
[btnLike setEnabled:YES];
}
}
該代碼使用Web服務,以更新的數喜歡特定的內容。當方法被放入單獨的ViewController類文件時,它可以正常工作,但是當我嘗試使用單個方法創建實用程序類時,我遇到了未調用方法didReceiveAuthenticationChallenge
,didReceiveResponse
,didReceiveData
和connectionDidFinishLoading
的問題。最初,我認爲委託方法會在文件中調用實用方法被調用。但事實並非如此。當我在實際的工具類中實現方法定義時,方法仍然沒有被調用。我做了一些關於這個主題的研究,並調查了this article,但發現我無法找到幫助我處理特定情況的大量資源。我如何設置我的實用課程?如果需要,我可以發佈實用程序的完整代碼。
您的自定義類是否符合NSURLConnectionDelegate? – serrrgi
由於您將NSURLConnection委託從靜態方法設置爲「self」,因此您將自定義類作爲委託傳遞給您,而不是傳遞您的自定義類的實例。將你的類方法轉換爲一個實例方法,它應該可以工作。 – serrrgi
不,我不這麼認爲,我正在閱讀NSURL文檔[文檔](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference的.html)。我如何使它符合NSURLConnectionDelegate? @serrrgi – ScottOBot