ios - 将数据传递给另一个 View Controller
<p><p>我有两个 ViewController (<code>DatePickerViewController</code> 和 <code>RouteHistoryViewController</code>)。我在 <code>DatePickerViewController</code> 中也有服务器响应。如何将该响应传递给 <code>RouteHitoryViewController</code>。 <code>RouteHistoryViewController</code> 有一个 mapView 。</p>
<p>这里是代码<code>DatePicker.m</code>:</p>
<pre><code>#import "DatePickerViewController.h"
#import "MapAnnotation.h"
@interface DatePickerViewController ()
@end
@implementation DatePickerViewController
{
//#define URL @"http://140e3087.ngrok.com"
#define URL3 @"http://784effb4.ngrok.com/bustracking/json/student/route_history"
NSString *formatedDate;
NSString *lat;
NSString *longi;
NSString *server_created_date;
}
@synthesize appDelagate,datePicker;
- (void)viewDidLoad {
;
AppDelegate *appDelegate = (AppDelegate *)[ delegate];
NSLog(@"%@", appDelegate);
NSDateFormatter *dateFormatter = [ init];
;
formatedDate = ;
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
;
// Dispose of any resources that can be recreated.
}
- (IBAction)sendPicker:(id)sender;
{
;
// NSLog(@"%@", formatedDate);
//self.selectedDate.text =formatedDate;
}
-(void) sendDataToServer : (NSString *) method{
NSString *beaconiD = @"EC112729B51B";
NSString *trackerID = @"e61078a67e4233ad";//appDelagate.tracker_id;
NSString *date = formatedDate;
NSMutableURLRequest *request = nil;
NSString *getURL = ;
getURL = ;
NSURL *url =;
request = ;
NSLog(@"link: %@", getURL);
;
;
NSURLConnection *connection = [ initWithRequest:request delegate:self];
NSLog(@"connection: %@", connection);
if( connection )
{
mutData = ;
}
else
{
NSLog (@"NO_CONNECTION");
return;
}
}
#pragma mark NSURLConnection delegates
-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *)response
{
;
}
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
;
}
-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog (@"NO_CONNECTION");
return;
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// NSString *jsonresultString =;
// NSLog(@"%@", jsonresultString);
// //serverResponse.text = jsonresultString;
NSError *error = nil;
NSDictionary *json = ;
NSArray *fetchedArr = ;
for (NSDictionary *user in fetchedArr)
{
lat = ;
longi = ;
server_created_date = ;
NSLog(@"Item date&time : %@", server_created_date);
NSLog(@"Item longitude : %@", longi);
NSLog(@"Item latitude : %@", lat);
}
}
</code></pre>
<p>这里是代码<code>RouteHistory.m</code>:</p>
<pre><code>#import "RouteHistoryViewController.h"
#import "MapAnnotation.h"
@interface RouteHistoryViewController ()
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@end
@implementation RouteHistoryViewController
- (void)viewDidLoad {
;
self.mapView.delegate = self;
//;
MapAnnotation *mapPoint = [ init];
mapPoint.coordinate = CLLocationCoordinate2DMake(, );
mapPoint.title = self.appDelagate.name;
mapPoint.time = self.appDelagate.server_created_date;
mapPoint.mapimage = self.appDelagate.image;
// Add it to the map view
;
// Zoom to a region around the pin
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(mapPoint.coordinate, 500, 500);
;
//testing
[ addObserver:self
selector:@selector(receiveTestNotification:)
name:@"MapUpdate"
object:nil];
// Do any additional setup after loading the view.
}
#pragma mark - MKMapViewDelegate
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
MKPinAnnotationView *view = nil;
static NSString *reuseIdentifier = @"MapAnnotation";
// Return a MKPinAnnotationView with a simple accessory button
view = (MKPinAnnotationView *);
if(!view)
{
view = [ initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
view.canShowCallout = YES;
view.animatesDrop = YES;
}
return view;
}
</code></pre>
<p>datepicker 使用 prepareforsegue 路由历史记录</p>
<pre><code>- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([isEqualToString:@"b"])
{
RouteHistoryViewController *rh = segue.destinationViewController;
NSLog(@"%@", rh);
}
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>在此行之后的第一个 ViewController 中的 connectionDidFinishLoading </p>
<pre><code> NSArray *fetchedArr = ;
</code></pre>
<p>添加以下两行</p>
<pre><code> _responseFromServer = fetchedArr;
;
</code></pre>
<p>然后在你的第一个 ViewController 中添加这个方法</p>
<pre><code> - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([ isEqualToString:@"segueToRouteHistory"])
{
RouteHistoryViewController *routeHistoryController = ;
;
}
}
</code></pre>
<p>在你的 First View Controller .h 文件中添加这个</p>
<pre><code>@property NSArray *responseFromServer;
</code></pre>
<p>现在我们已经将从服务器接收到的响应数组分配给目标 ViewController 中的一个对象。</p>
<p>别忘了添加</p>
<pre><code>@property NSArray *fetchedArray;
</code></pre>
<p>在您的第二个 ViewController 的 .h 文件中</p>
<p>现在您可以在第二个 ViewController 中访问该数组。
PS:不要忘记将 Storyboard从第一个 ViewController 转移到第二个 ViewController ,并将 Segue Identifier 命名为“segueToRouteHistory”</p></p>
<p style="font-size: 20px;">关于ios - 将数据传递给另一个 ViewController ,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/34303612/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/34303612/
</a>
</p>
页:
[1]