0

したがって、データを PHP スクリプトに送信するコードがあります。アイデアは、コードがデータをデータベースに挿入することです。これが成功した場合、返される文字列は「SUCCESSFUL」になり、それ以外の場合は「Failed」になります。成功した値が返されたときに、ありがとう(ラベル)という別のビューをロードする必要があります。どうすればこれを達成できますか?

コード:

(IBAction)saveDataAction:(id)sender {
    NSString *lEventTitle = [NSString stringWithFormat:@"var=%@",eventTitle.text];
    NSString *lEventDesc = [NSString stringWithFormat:@"var=%@",eventDescription.text];
   NSString *lEventCity = [NSString stringWithFormat:@"var=%@",eventCity.text];



    // Create your request string with parameter name as defined in PHP file
    NSString *myRequestString = [NSString stringWithFormat:@"title=%@&description=%@&city=%@",lEventTitle ,lEventDesc,lEventCity];

    myRequestString  = [myRequestString stringByReplacingOccurrencesOfString:@"var=" withString:@""];
    NSLog(@"%@",myRequestString);
    // Create Data from request
    NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://www.xsysdevelopment.com/ios/form.php"]];
    // set Request Type
    [request setHTTPMethod: @"POST"];
    // Set content-type
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    // Set Request Body
    [request setHTTPBody: myRequestData];
    // Now send a request and get Response
    NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
    // Log Response
    NSString *response = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding];
    NSLog(@"%@",response);

    if ([response rangeOfString:@"SUCCESSFUL"].location == NSNotFound)
    {
        //here??
    }

}

事前に助けてくれてありがとう

4

2 に答える 2

0
if ([response rangeOfString:@"SUCCESSFUL"].location != NSNotFound)
{
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Thank you" delegate:self cancelButtonTitle:@"Close" otherButtonTitles:@"OK", nil];
     [alert show];
     [alert release];
}

ここでアラートが表示され、このようなデリゲート メソッドを処理できるようになりました。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex==1) //OK button pressed
    {
        //handle your another view here
         SampleViewController *sampleView = [[[SampleViewController alloc] init] autorelease];
         [self presentModalViewController:sampleView animated:YES];
    }
}

UIAlertViewDelegate必ず.h ファイルに実装してください。

ビューの表示の詳細については、リファレンスを参照してください。

  1. iOS 用コントローラ プログラミング ガイドを表示
  2. モーダル ビュー コントローラーの例
于 2013-03-24T16:10:23.530 に答える
0

ナビゲーションベースのアプリケーションを使用している場合は、次を使用します

if ([response rangeOfString:@"SUCCESSFUL"].location != NSNotFound)
{
    SuccessViewController *objSVC = [[SuccessViewController alloc] initWithNibName:@"SuccessViewController" bundle:[NSBundle mainBundle]];
    [self.navigationController pushViewController:objSVC animated:YES];
}
else
{
    FailViewController *objFVC = [[FailViewController alloc] initWithNibName:@"FailViewController" bundle:[NSBundle mainBundle]];
    [self.navigationController pushViewController:objFVC animated:YES];        
}

それ以外の場合は交換

[self.navigationController pushViewController:objFVC animated:YES];

[self presentModalViewController:objFVC animated:YES];
于 2013-03-24T17:00:10.940 に答える