我所做的當前程序允許用戶將文本輸入到按下提交按鈕時將數據發送到服務器的文本框中。使用ObjectiveC中的多個視圖控制器向服務器發送數據
但是,我有多個視圖控制器,每個視圖控制器都有自己的輸入框供用戶輸入數據。例如:VC1有一個ID和密碼文本框,VC2有一個年齡文本框,VC3有一個國籍文本框。
是否可以一次提交來自每個VC的所有數據?我只使用一個PHP文件來做到這一點嗎?我很困惑這個概念,所以任何幫助都會很棒!
我的PHP代碼:
<?php
$username = "root";
$database = "testdb";
mysql_connect('localhost', $username);
@mysql_select_db($database) or die ("Unable to find database");
$name = @$_GET["name"];
$message = @$_GET["message"];
$query = "INSERT INTO test VALUES ('', '$name', '$message')";
mysql_query($query) or die (mysql_error("error"));
mysql_close();
?>
和我的OBJç .H
#import <UIKit/UIKit.h>
#define kPostURL @"http://localhost/TESTCONNECT.php" //variable to use whenever
#define kName @"name"
#define kMessage @"message"
@interface FirstViewController : UIViewController{
IBOutlet UITextField *nameText;
IBOutlet UITextView *messageText;
NSURLConnection *postConnection;
}
-(IBAction)post:(id)sender;
@end
.M
#import "FirstViewController.h"
@interface FirstViewController()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void) postMessage:(NSString*) message withName:(NSString *) name{
if(name !=nil && message !=nil){
NSMutableString *postString = [NSMutableString stringWithString:kPostURL];
[postString appendString:[NSString stringWithFormat:@"?%@=%@", kName, name]];
[postString appendString: [NSString stringWithFormat:@"&%@=%@", kMessage, message]];
[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];
}
}
-(IBAction)post:(id)sender{
[self postMessage:messageText.text withName:nameText.text];
[messageText resignFirstResponder];
messageText.text=nil;
nameText.text=nil;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
將數據從一個視圖傳遞到另一個視圖並僅使用最後一個視圖提交它並不是一個壞主意。謝謝!我會試一下! – Pammuelo 2014-11-03 11:51:47
快速的問題,如果我這樣做,我可以直接將它傳遞給最後一個視圖?或者它是否需要通過每一個? – Pammuelo 2014-11-05 00:41:33