菜鸟教程小白 发表于 2022-12-13 12:39:28

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 &#34;DatePickerViewController.h&#34;
#import &#34;MapAnnotation.h&#34;

@interface DatePickerViewController ()


@end

@implementation DatePickerViewController
{
    //#define URL @&#34;http://140e3087.ngrok.com&#34;
#define URL3 @&#34;http://784effb4.ngrok.com/bustracking/json/student/route_history&#34;
    NSString *formatedDate;
    NSString *lat;
    NSString *longi;
    NSString *server_created_date;


}
@synthesize appDelagate,datePicker;

- (void)viewDidLoad {
    ;
    AppDelegate *appDelegate = (AppDelegate *)[ delegate];
    NSLog(@&#34;%@&#34;, 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(@&#34;%@&#34;, formatedDate);

    //self.selectedDate.text =formatedDate;
}

-(void) sendDataToServer : (NSString *) method{


    NSString *beaconiD = @&#34;EC112729B51B&#34;;
    NSString *trackerID = @&#34;e61078a67e4233ad&#34;;//appDelagate.tracker_id;
    NSString *date = formatedDate;


    NSMutableURLRequest *request = nil;
    NSString *getURL = ;
    getURL = ;
    NSURL *url =;
    request = ;

    NSLog(@&#34;link: %@&#34;, getURL);

    ;
    ;

    NSURLConnection *connection = [ initWithRequest:request delegate:self];

    NSLog(@&#34;connection: %@&#34;, connection);

    if( connection )
    {
      mutData = ;
    }
    else
    {
      NSLog (@&#34;NO_CONNECTION&#34;);
      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 (@&#34;NO_CONNECTION&#34;);

    return;
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{

//      NSString *jsonresultString =;
//      NSLog(@&#34;%@&#34;, jsonresultString);
//      //serverResponse.text = jsonresultString;

      NSError *error = nil;
      NSDictionary *json = ;
      NSArray *fetchedArr = ;


      for (NSDictionary *user in fetchedArr)
      {


            lat = ;
            longi = ;
            server_created_date = ;

            NSLog(@&#34;Item date&amp;time : %@&#34;, server_created_date);
            NSLog(@&#34;Item longitude : %@&#34;, longi);
            NSLog(@&#34;Item latitude : %@&#34;, lat);
      }



}
</code></pre>

<p>这里是代码<code>RouteHistory.m</code>:</p>

<pre><code>#import &#34;RouteHistoryViewController.h&#34;
#import &#34;MapAnnotation.h&#34;

@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:@&#34;MapUpdate&#34;
                                             object:nil];


    // Do any additional setup after loading the view.
}
#pragma mark - MKMapViewDelegate

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id&lt;MKAnnotation&gt;)annotation
{
    MKPinAnnotationView *view = nil;
    static NSString *reuseIdentifier = @&#34;MapAnnotation&#34;;

    // 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:@&#34;b&#34;])
    {
      RouteHistoryViewController *rh = segue.destinationViewController;


      NSLog(@&#34;%@&#34;, 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:@&#34;segueToRouteHistory&#34;])
{


   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]
查看完整版本: ios - 将数据传递给另一个 View Controller