2014-11-03 70 views
0

我所做的當前程序允許用戶將文本輸入到按下提交按鈕時將數據發送到服務器的文本框中。使用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 

回答

0

如何你的域顯示?他們都是單一形式還是多種形式?

你可以在一個單一的形式顯示所有字段,然後在表格所有數據都將在同一$ _ POST通過提交按鈕在窗體上的形式提交給操作參數。

如果字段都舉行不同形式在同一頁面上,你可以使用JavaScript來收集所有的數據,並同時提交了這一切。

如果所有的領域都不同看法/頁,周圍的工作,以便能夠通過所有數據的一次;可能會將$ _POST數據從一個視圖傳遞到下一個視圖,並將其放在隱藏字段中,以便在最後一步中,您可以一次提交所有數據。

+0

將數據從一個視圖傳遞到另一個視圖並僅使用最後一個視圖提交它並不是一個壞主意。謝謝!我會試一下! – Pammuelo 2014-11-03 11:51:47

+0

快速的問題,如果我這樣做,我可以直接將它傳遞給最後一個視圖?或者它是否需要通過每一個? – Pammuelo 2014-11-05 00:41:33

0

即使您的URL參數存在於多個MVC,作爲「登錄」句柄,您應該立即訪問php 文件。 如果您想要使用參數,只需創建一個.h文件,使用「extern」定義全局變量,然後將它們分配給您的多個MVC,以便獲取數據。

0

你必須在appdelegate中創建一個字典,並在每個視圖控制器中使用鍵保存字段,並且在最終視圖控制器中,您可以從dictionary1提交數據到服務器>在AppDelegate中創建Dictionary作爲PostDataDictionary; 2>現在在你的視圖控制器[PostdataDictionary setObject:@「」objctforkey:@「」];爲每個視圖設置這樣的對象控制器現在在Final View Controller中ASIFormDataRequest * request; request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@「」]]]; [request setDelegate:self];

 [request setPostValue:[appDelegate.PostDataDictionary objectForKey:@"FirstName"] forKey:@"fname"]; 
     [request setPostValue:[appDelegate.PostDataDictionary objectForKey:@"LastName"] forKey:@"lname"]; 

     [request addData:imagedata withFileName:@"file1.png" andContentType:@"image/jpeg" forKey:@"photo"]; 
     [request startAsynchronous]; 
    } 
} 
+0

請在答案中包含一些實用的代碼。 – 2014-11-03 06:23:26

相關問題