我对IOS很新。在另一个人的帮助下,设法从第三方API“获取”数据并显示在我的表视图中,但现在我想从UIText字段中编写一些字符串,根据API文档进行格式化并写入服务器。我使用块的现有代码。任何人都可以提供一个非常简单的例子吗?
以下是在黑暗中粗暴地分解所需的东西:
Class 1 - VC class:
//User input from UIText field
我假设我需要将字符串写入NSDictionary-对象和密钥对,并使用块?
调用位于Class 2
的方法,该方法将从Class 1
中的块请求数据?
第2类 - 模型类:
//Format the data as per the API dictates
//'PUT' the string to the server
这是我到目前为止所做的:
URLOUTViewController.h类
//
// URLOUTViewController.h
// URLOUT
//
// Created by Gregory Arden on 21/11/2013.
// Copyright (c) 2013 Not Defined. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "URLOUT_Model.h"
@interface URLOUTViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *firstName;
@property (strong, nonatomic) IBOutlet UITextField *secondName;
- (IBAction)submitButton:(id)sender;
@end
URLOUTViewController.m
//
// URLOUTViewController.m
// URLOUT
//
// Created by Gregory Arden on 21/11/2013.
// Copyright (c) 2013 Not Defined. All rights reserved.
//
#import "URLOUTViewController.h"
#import "URLOUT_Model.h"
@interface URLOUTViewController ()
//Private properties
@property (strong, nonatomic) URLOUT_Model *modelNewDroplet;
@property (strong, nonatomic) NSDictionary *userInput;
@end
@implementation URLOUTViewController
@synthesize userInput;
@synthesize firstName;
@synthesize secondName;
- (URLOUT_Model *) modelNewDroplet
{
if (!_modelNewDroplet) _modelNewDroplet = [[URLOUT_Model alloc]init];
return _modelNewDroplet;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[URLOUT_Model createDropletWithCompletion:^(NSDictionary *userInput) {
self.modelNewDroplet = userInput;
}];
// 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)submitButton:(id)sender {
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:
self.firstName.text, @"firstName", self.secondName.text, @"secondname",nil];
NSLog(@"%@", [dic objectForKey:@"firstName, secondName"]);
}
@end
URLOUT_Model.h
//
// URLOUT_Model.h
// URLOUT
//
// Created by Gregory Arden on 21/11/2013.
// Copyright (c) 2013 Not Defined. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "URLOUTViewController.h"
typedef void (^NSDictionayBlock)(NSDictionary * params);
@interface URLOUT_Model : NSObject
+ (void)createDropletWithCompletion:(NSDictionary *) params;
@end
URLOUT_Model.m
//
// URLOUT_Model.m
// URLOUT
//
// Created by Gregory Arden on 21/11/2013.
// Copyright (c) 2013 Not Defined. All rights reserved.
//
#import "URLOUT_Model.h"
@implementation URLOUT_Model
+ (void)createDropletWithCompletion:(NSDictionary *) params;
{
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:@{params: firstName, secondName, nil}];
[urlRequest setHTTPMethod:@"PUT"];
[urlRequest setCachePolicy:NSURLCacheStorageNotAllowed];
[urlRequest setTimeoutInterval:30.0f];
[urlRequest addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[NSURLConnection sendAsynchronousRequest:urlRequest
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *responseData, NSError *error){
NSError *serializationError = nil;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData
options:NSJSONReadingAllowFragments
error:&serializationError];
}];
}
@end
答案 0 :(得分:0)
创建HTTP PUT请求很简单:
创建NSMutableURLRequest
。
使用@"PUT"
方法将HTTP方法设置为-setHTTPMethod:
。
使用适当的方法(-setValue:forHTTPHeaderField:
,-setHTTPBody:
等设置标题字段和正文。)
使用NSURLConnection
。
您在请求中发送的数据来自(文本字段,数据文件,随机数生成器......),这对请求的创建没有任何影响。但是,您确实不应该在视图中存储数据。一般来说,文本视图等应在视图控制器发生更改时触发操作,视图控制器应更新数据模型。如果要将数据发送到某个服务器,请从模型中提取数据,而不是从UI组件中提取数据。