结果正确时加载另一个视图控制器

时间:2013-03-24 16:02:50

标签: ios objective-c

所以我将代码提交给PHP脚本。想法是代码将数据插入数据库。这成功发生,返回的字符串将是“SUCCESSFUL”,否则,“失败”。现在我想要当成功的值返回时,它会加载另一个视图,说谢谢(标签)。我怎样才能做到这一点?

代码:

(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??
    }

}

感谢您的帮助

2 个答案:

答案 0 :(得分: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];
    }
}

确保在.h文件中实施UIAlertViewDelegate

有关呈现视图的更多信息。

  1. View Controller Programming Guide for iOS
  2. Modal View Controller Example

答案 1 :(得分: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];