我試圖從幾個已添加到故事板的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
第一個問題:您確定IBOutlets是否已插入界面構建器中? – aout
是的,我仔細檢查了 – Yamaha32088
好吧,現在我看到在sendRequest中實際上創建了ViewController的新實例,但原始ViewController是如何創建的?它是從IB創建並顯示的,是否是初始視圖控制器? – aout