【问题标题】:Xcode HTTP GET REQUEST between views视图之间的 Xcode HTTP GET REQUEST
【发布时间】:2012-10-11 19:03:34
【问题描述】:
我对 xcode 非常陌生,并且知道一点编码,但我正在尝试在我的应用程序中实现 api 的使用。这就是我想要做的。
- 从我的第一个视图控制器获取地理位置。
- 从我的第二个视图控制器中获取几个变量。
- 使用所有收集的变量并生成我的 HTTP 请求
- 在我的第三个视图控制器上显示返回的数据。
我已经设置了我的视图控制器,并且我的第一个视图控制器已经找到了我。
任何帮助将不胜感激。我在山狮上使用最新的xcode,这是需要发送的请求http://yourtaximeter.com/api/?key=...
【问题讨论】:
标签:
iphone
xcode
http
geolocation
xmlhttprequest
【解决方案2】:
在请求前的VC上,声明一个属性(和@synthesize)来保存网络请求的结果。
@property (nonatomic, strong) NSData *responseData;
然后在触发请求的任何事件上,像这样启动它:
NSString *urlString = /* form the get request */
NSURL *url = [NSURL urlWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// consider doing some UI on this VC to indicate that you're working on a request
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (!error) {
self.responseData = data;
// hide the "busy" UI
// now go to the next VC with the response
[self performSegueWithIdentifier:@"ThridVCSegue" sender:self];
}
}];
然后像这样将响应数据传递给第三个 VC:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"ThridVCSegue"]) {
ThirdViewController *vc = (ThirdViewController *)[segue destinationViewController];
[vc dataFromHTTPRequest:self.responseData];
}
}
这假设您将使用 ARC、故事板并定义该转场。 ThirdViewController 需要一个公共方法来接受 http 响应数据。