わかりました、これは非常に単純なはずです - 私がしたいのは、私の ServerConnect.m (NSObject)、私の SignIn.m (ViewController) から NSURL 接続要求メソッドを呼び出し、NSURL 要求が完了した後に UIActivityIndicatorView を停止することだけです。もちろん、メインスレッドですべてを行う場合:
- (IBAction)forgotPassword:(id)sender {
[activityIndicator startAnimating];
connection = [[ServerConnect alloc] init];
[connection sendUserPassword:email withSecurity:securityID];
[activityIndicator stopAnimating];
}
次に、すべてが同時に実行され、接続メソッドが終了する前にアクティビティインジケーターが開始および停止します...
したがって、接続要求をセカンダリ スレッドに配置しようとしました。
- (IBAction)forgotPassword:(id)sender {
[NSThread detachNewThreadSelector: @selector(requestNewPassword:) toTarget:self withObject:userEmail.text];
}
- (void) requestNewPassword:(NSString *)email
{
[self->thinkingIndicator performSelectorOnMainThread:@selector(startAnimating) withObject:nil waitUntilDone:NO];
//Make NSURL Connection to server on secondary thread
NSString *securityID = [[NSString alloc] init];
securityID = @"security";
connection = [[ServerConnect alloc] init];
[connection sendUserPassword:email withSecurity:securityID];
[self->thinkingIndicator performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:NO];
}
しかし、ここにもアクティビティ インジケータが表示されません。これは、セカンダリ スレッドで NSURL リクエストが適切に機能していないことが原因である可能性があります (つまり、何らかの理由で、メイン スレッドでリクエストされたときのように xml 文字列を収集しません)。 .
これを機能させるためにコードを構築する適切な方法は何ですか? 別のファイルのメソッドの実行が終了した後、アクティビティ インジケーターを単純に停止させる方法を見つけようとするのに、どれだけの労力が費やされたかに驚いています。コードを同時にではなく連続して (次々に) 実行する方法はありますか? どんな助けでも大歓迎です。
次のように更新: sendUserPassword:(NSString *)withSecurity:(NSString *)
- (void)sendUserPassword:(NSString *)emailString
withSecurity:(NSString *)passCode;
{
NSLog(@"Making request for user's password");
newUser = NO;
fbUser = NO;
forgotPassword = YES;
NSString *post = [NSString stringWithFormat: @"email=%@&s=%@", emailString, passCode];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
//Construct the web service URL
NSURL *url = [NSURL URLWithString:@"http://www.someurl.php"];
//Create a request object with that URL
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:90];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];
//Clear out the existing connection if there is one
if(connectionInProgress) {
[connectionInProgress cancel];
}
//Instantiate the object to hold all incoming data
xmlData = [[NSMutableData alloc] init];
//Create and initiate the conection - non-blocking
connectionInProgress = [[NSURLConnection alloc] initWithRequest: request
delegate:self
startImmediately:YES];
}