2013-03-24 80 views
0

所以我有代碼提交數據到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?? 
    } 

} 

感謝您的幫助提前

+0

是否要提示消息結果? – 2013-03-24 16:04:39

+0

是的,這將是非常好的。然後當用戶點擊確定...它加載另一個單獨的視圖,即主視圖。 – 2013-03-24 16:05:44

回答

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
+0

是隊友,但加載其他視圖是我不知道該怎麼做,並尋求幫助英寸更多的解釋或我可以在網上閱讀? – 2013-03-24 16:14:54

+0

請看最新的答案 – 2013-03-24 16:46:40

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]; 
相關問題