如何将在不同文本字段中输入的值存储到目标C中的服务器中,在我的项目中,我创建了一个表单,其中包含不同的文本字段,用户必须在文本字段中输入值,我保留了一个SAVE按钮,在输入文本字段的值后,用户必须单击“保存”按钮。
我必须在点击SAVE按钮时将输入文本字段的值保存到服务器。
所以如何在点击SAVE按钮时将数据或值保存到服务器。
以下是我用来创建表单的代码,
在.h文件中:
#import <UIKit/UIKit.h>
#import "PickerViewController.h"
@interface PopAppViewController : UIViewController < NumberPickedDelegate>{
UIPopoverController *popOverController;
UIPopoverController *popOverControllerWithPicker;
PickerViewController *pickerViewController;
IBOutlet UITextField *txtTest;
IBOutlet UITextField *txtSun;
IBOutlet UITextField *txtMon;
IBOutlet UITextField *txtTue;
IBOutlet UITextField *txtWed;
IBOutlet UITextField *txtThurs;
IBOutlet UITextField *txtFri;
IBOutlet UITextField *txtSat;
IBOutlet UITextField *txtTotal;
IBOutlet UITextField *txtTask;
IBOutlet UITextField *txtProject;
}
@property (nonatomic, retain) UIPopoverController *popOverController;
@property (nonatomic, retain) UIPopoverController *popOverControllerWithPicker;
@property (nonatomic, retain) PickerViewController *pickerViewController;
@property (nonatomic, retain) UITextField *txtTest;
@property (nonatomic, retain) UITextField *txtSun;
@property (nonatomic, retain) UITextField *txtMon;
@property (nonatomic, retain) UITextField *txtTue;
@property (nonatomic, retain) UITextField *txtWed;
@property (nonatomic, retain) UITextField *txtThurs;
@property (nonatomic, retain) UITextField *txtFri;
@property (nonatomic, retain) UITextField *txtSat;
@property (nonatomic, retain) UITextField *txtTotal;
@property (nonatomic, retain) UITextField *txtTask;
@property (nonatomic, retain) UITextField *txtProject;
-(IBAction)displayPickerPopover;
-(IBAction)exit;
-(IBAction)reset;
-(IBAction)save;
-(IBAction)total;
@end
在.m文件中:
#import "PopAppViewController.h"
//#import "TimeSheetDatabase.h"
@implementation PopAppViewController
@synthesize popOverController,popOverControllerWithPicker,pickerViewController,txtTest,txtSun,txtMon,txtTue,txtWed,txtThurs,txtFri,txtSat,txtTotal,txtTask,txtProject;
//-(id)initWithtxtProject:(NSString *)txtProject txtTask:(NSString *)txtTask txtSun:(int)txtSun txtMon:(int)txtMon txtTue:(int)txtTue txtWed:(int)txtWed txtThurs:(int)txtThurs txtFri:(int)txtFri txtSat:(int)txtSat txtTotal:(int)txtTotal{
//
// self=[super init];
// if(self){
// self.txtProject = txtProject;
// self.txtTask = txtTask;
// self.txtSun = txtSun;
// self.txtMon = txtMon;
// self.txtTue = txtTue;
// self.txtWed = txtWed;
// self.txtThurs = txtThurs;
// self.txtFri = txtFri;
// self.txtSat = txtSat;
// self.txtTotal = txtTotal;
//
// }
//}
-(IBAction)displayPickerPopover {
[txtTest resignFirstResponder];
CGSize sizeOfPopover = CGSizeMake(300, 422);
CGPoint positionOfPopover = CGPointMake(32, 325);
[popOverControllerWithPicker presentPopoverFromRect:CGRectMake(positionOfPopover.x, positionOfPopover.y, sizeOfPopover.width, sizeOfPopover.height)
inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
-(IBAction)exit{
exit(0);
}
-(IBAction)reset{
txtSun.text = @"";
txtMon.text = @"";
txtTue.text = @"";
txtWed.text = @"";
txtThurs.text = @"";
txtFri.text = @"";
txtSat.text = @"";
txtTotal.text = @"";
txtTest.text = @"";
txtTask.text = @"";
}
-(IBAction)save{
}
-(IBAction)total{
int result = [txtSun.text intValue] + [txtMon.text intValue] + [txtTue.text intValue] + [txtWed.text intValue] + [txtThurs.text intValue] + [txtFri.text intValue] + [txtSat.text intValue];
txtTotal.text = [NSString stringWithFormat:@"%d",result];
}
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
-(void)numberDidChangeTo:(NSString *)newNumber {
txtTest.text = newNumber;
}
-(void)didChangeSelection:(NSString *)newValue {
txtTest.text = newValue;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
pickerViewController = [[PickerViewController alloc] init];
pickerViewController.delegate = self;
popOverControllerWithPicker = [[UIPopoverController alloc] initWithContentViewController:pickerViewController];
popOverController.popoverContentSize = CGSizeMake(300, 216);
// NSArray *timesheetinfo = [[TimeSheetDatabase database]getAllTimeSheet];
// for(timesheetinfo *info in timesheetinfo){
//
// NSLog(@"%@ - %@ ",info.project,info.task);
// }
[super viewDidLoad];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[popOverController release];
[popOverControllerWithPicker release];
[pickerViewController release];
[txtTest release];
[super dealloc];
}
@end
答案 0 :(得分:0)
您需要将数据编译为JSON字符串,然后使用NSURLRequest
-(IBAction)save
{
// build JSON string
NSDictionary *postDictionary = [NSDictionary dictionaryWithObjectsAndKeys:self.txtTest.text, @"test",
self.txtSun.text, @"sun",
self.txtSun.text, @"mon",
nil];
NSData *postData = [NSJSONSerialization dataWithJSONObject:postDictionary options:NSJSONWritingPrettyPrinted error:NULL];
// perform http request (on a background thread)
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:@"http://example.com/save.php" cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];
NSHTTPURLResponse *urlResponse = nil;
NSError *error = NULL;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
// and now go back to the main thread
dispatch_async(dispatch_get_main_queue(), ^{
NSAutoreleasePool *mainQueuePool = [[NSAutoreleasePool alloc] init];
// debug: print response
NSLog(@"%@", [[NSString alloc] initWithData:responseData encoding:NSISOLatin1StringEncoding]);
// check for http error (this includes php exceptions)
if ([urlResponse statusCode] != 200) {
NSLog(@"save failed with status code != 200");
return;
}
[mainQueuePool release];
});
[pool release];
});
}
在你的php中:
$rawData = file_get_contents("php://input");
$postData = json_decode($rawData);
print_r($postData);
答案 1 :(得分:0)
由于Objective-C支持纯C,您可以使用类似here描述的C库连接到MySQL服务器。