0

同じビューに異なるアラート ビューを表示する 2 つのボタンがあり、異なる値をデータベースに渡します。どちらも個別に機能します。問題は、ボタン 1 をクリックし、ボタン 2 の後にボタン 2 の詳細が送信されない場合と、その逆の場合です。これを修正する方法がわかりません。何かアイデアはありますか?

使用しているコード:

-(void) addNewComment:(NSString*) newComment withName:(NSString *) routeID{

if (newComment != nil){

    NSMutableString *postString = [NSMutableString stringWithString:postCommentURL];

    [postString appendString:[NSString stringWithFormat:@"?%@=%@", SnewComment, newComment]];
    [postString appendString:[NSString stringWithFormat:@"&%@=%@", SrouteID, routeID]];
    [postString setString:[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]];
    [request setHTTPMethod:@"POST"];

    postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}
}

-(void) updateRating:(NSString*) newRating withName:(NSString *) newRateNo withName:(NSString *) routeID{
//similar to the above
}


- (IBAction)addCommentButton:(id)sender{

UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Add Comment" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add Comment",nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
alert.tag = 1;
[alert show];
}

- (IBAction)rateButton:(id)sender{

//similar to the above
alert.tag = 2;
[alert show];
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alertView.tag == 1)
{
    if (buttonIndex == 0) {
    NSLog(@"Cancel button clicked");
}
    if (buttonIndex == 1) {

    self.addNewCommentString = [[alertView textFieldAtIndex:0] text];

    self.allComments = [NSString stringWithFormat:@"%@\n\n%@",commentTextView.text, self.addNewCommentString];
    NSLog(@"%@%@",routeIDLabel ,allComments);

    [self addNewComment:self.allComments withName:routeIDLabel.text];
    [routeIDLabel resignFirstResponder];
    allComments = nil;
    routeIDLabel.text = nil;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Test1" object:self];

    }
} else {
    //similar to the above
}
}
4

1 に答える 1

1

パラメータをクエリ文字列で送信しているようです。POST の代わりに GET を試すことができます。

キャッシュ ポリシーの設定を試すことができます。

NSURL *url = [NSURL URLWithString: postString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:15];

リクエストを送信して応答を待つこともできます (理想的ではありませんが、少なくとも違いがあるかどうかを確認するには)。

NSError *error;
NSURLResponse *response;
NSData *data = [NSURLConnection sendSynchronousRequest:request 
                                     returningResponse:&response
                                                 error:&error];
于 2014-03-20T23:19:26.937 に答える