2015-05-13 47 views
0

我試圖從幾個已添加到故事板的UITextField中獲取值,並將它們作爲JSON字符串發送到API。我可以硬編碼的JSON字符串的值,一切工作正常。現在我想從文本字段中檢索值並將它們插入硬編碼值中。問題是,當我將這些值記錄到控制檯時,它們顯示爲空白。我不確定我是否有正確的代碼將ViewController的值傳遞給發送數據的方法。我遵循了幾個關於如何從ViewController UITextFields獲取數據的教程。我將文本字段連接到ViewController.h文件中的屬性。我希望有人能幫我弄清楚我做錯了什麼,希望我提供了足夠的信息。Objective C UITextField值爲空

我認爲這個問題可能是我怎麼想從代碼中timeMethods.m文件中的這些行獲取值:

ViewController *controller = [[ViewController alloc]initWithNibName:@"ViewController" bundle:nil]; 

    NSString *name = controller.name.text; 
    NSString *type = controller.type.text; 
    NSString *date = controller.date.text; 

ViewController.h

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController 

@property (nonatomic, retain) IBOutlet UITextField *name; 
@property (nonatomic, retain) IBOutlet UITextField *type; 
@property (nonatomic, retain) IBOutlet UITextField *date; 

@end 

ViewController.m

#import "ViewController.h" 
#import "timeMethods.h" 

@interface ViewController() 

@end 

@implementation ViewController 

@synthesize name; 
@synthesize type; 
@synthesize date; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 
- (IBAction)sendRequest:(id)sender { 
    [[timeMethods alloc] sendRequest]; 
} 

- (IBAction)getRequest:(id)sender { 
    [[timeMethods alloc] getRequest]; 
} 


@end 

timeMethods.m

#import "timeMethods.h" 

@implementation timeMethods 

- (void)sendRequest { 

    ViewController *controller = [[ViewController alloc]initWithNibName:@"ViewController" bundle:nil]; 

    NSString *name = controller.name.text; 
    NSString *type = controller.type.text; 
    NSString *date = controller.date.text; 

    NSURL *url = [NSURL URLWithString:@"http://throttle.com/my-rest-api/api/robots"]; 
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; 

    NSDictionary *tmp = [[NSDictionary alloc] initWithObjectsAndKeys: 
         name, @"name", 
         type, @"type", 
         date, @"year", 
         nil]; 
    NSError *error; 
    NSData *postData = [NSJSONSerialization dataWithJSONObject:tmp options:0 error:&error]; 
    [request setHTTPBody:postData]; 

    [request setHTTPMethod:@"POST"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
    [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[postData length]] forHTTPHeaderField:@"Content-Length"]; 
    [request setHTTPBody: postData]; 
    NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self]; 
    NSString *strData = [[NSString alloc]initWithData:postData encoding:NSUTF8StringEncoding]; 
    NSLog(@"%@", strData); 
} 

- (void)getRequest { 
    NSString *serverAddress = @"http://throttle.com/my-rest-api/api/robots"; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:serverAddress] 
                  cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData 
                 timeoutInterval:10]; 
    [request setHTTPMethod:@"GET"]; 
    NSError *requestError; 
    NSURLResponse *urlResponse = nil; 
    NSData *response1 = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&requestError]; 
    NSString *strData = [[NSString alloc]initWithData:response1 encoding:NSUTF8StringEncoding]; 
    NSLog(@"%@",strData); 
} 

@end 

timeMethods.h

#import <Foundation/Foundation.h> 

@interface timeMethods : NSObject 

- (void)sendRequest; 
- (void)getRequest; 

@end 
+0

第一個問題:您確定IBOutlets是否已插入界面構建器中? – aout

+0

是的,我仔細檢查了 – Yamaha32088

+0

好吧,現在我看到在sendRequest中實際上創建了ViewController的新實例,但原始ViewController是如何創建的?它是從IB創建並顯示的,是否是初始視圖控制器? – aout

回答

3

麻煩的是,我們剛剛分配了一個全新的VC,並要求其的UITextField的價值觀在這裏:

ViewController *controller = [[ViewController alloc]initWithNibName:@"ViewController" bundle:nil]; 

// controller.name is a brand new text field, created in the previous line 
NSString *name = controller.name.text; // guaranteed to be @"" 

當然,這將是一個空文本域。它在上一行中創建。取而代之的是,獲得這些值PARAMS從用戶使用視圖控制器您的請求......

- (IBAction)sendRequest:(id)sender { 

    NSString *name = self.name.text; 
    NSString *type = self.type.text; 
    NSString *date = self.date.text; 

    [[timeMethods alloc] sendRequestWithName:name type:type date:date]; 
} 

當然,這些參數添加到sendRequest方法和刪除本地變量。

+0

這應該是實現它的好方法。我還會改變sendRequest的定義方式(也許使用靜態方法) – aout

+0

@aout,好主意。此外,只注意到網點被宣佈爲「保留」。這是「強」的同義詞,但有點過時。我更喜歡'strong',因爲它向讀者表明它是一個ARC項目。 @synthesize行也可能是不必要的。 – danh

+0

同意,但一個弱關係應該做這項工作,因爲這個觀點已經對它裏面的所有東西保持了強有力的參考。 – aout