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