0

我有两个按钮,它们在同一个视图中显示不同的警报视图,它们将不同的值传递给数据库,两者都单独工作。问题是,如果我单击按钮 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

看起来您正在查询字符串中发送参数。您可以尝试 GET 而不是 POST。

您可以尝试设置缓存策略:

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 回答