2011-11-14 13 views
0

異なるテキストフィールドに入力された値を目的のCでサーバーに保存する方法私のプロジェクトでは、ユーザーがテキストフィールドに値を入力する必要がある別のテキストフィールドで構成されています。私は1つのSAVEボタンを保持しています。テキストフィールドに値を入力した後、ユーザーはSaveボタンをクリックします。異なるテキストフィールドに入力された値を目的のCのサーバーに保存する方法

SAVEボタンをクリックすると、テキストフィールドに入力した値をサーバーに保存する必要があります。

SAVEボタンをクリックすると、データや値をサーバーに保存する方法を教えてください。あなたがコンパイルする必要が

#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

「サーバー」の定義は何ですか?それはWebサービス、データベース、またはxmlファイルをサーバーに保存しますか?この質問に対する答えは、サーバーが意味することを知らなければ不可能です。 –

+0

こんにちはyan.kun、私はそれをmysqlデータベースに保存します。 – shasha

答えて

0

#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ファイルで:

には、以下の.hファイルで、私は、フォームを作成するために使用したコード、

ですデータを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); 
+0

問題のサーバーでPHPが実行されると思いますか? – JeremyP

+0

私の回答を編集して他の言語の同等のものを自由に追加してください。 PHP以外の言語でPOSTからJSONデータを読み込む方法がわかりません。 –

+0

こんにちはアビ、私は、次のリンクの動画を見て、私はPHPを介してmampのようなローカルサーバーにデータを格納しようとしています - http://www.youtube.com/watch?v=LwCyaGoRNWM、しかし私はプロジェクトにPHPファイルを追加する方法や、プロジェクトのPHPコードを書く場所を知っている、私は開発側に新しいです、私の質問はあなたに簡単に見えるかもしれません、私を助けてください。 – shasha

0

のObjective-Cは、純粋なCをサポートしているため、あなたがMySQLサーバに接続するために説明しhereのようなCライブラリを使用することができます。

関連する問題