将数据传递给视图控制器

时间:2016-02-24 14:08:15

标签: ios objective-c

这是我传递数据的按钮功能。

问题是区域和国家/地区详细信息会传递给下一个ViewController,但纬度和经度会返回null。

在ViewController.m中:

- (IBAction)forecast:(UIButton *)sender
{
    ForecastViewController *sendDetails = [[ForecastViewController alloc] init];
    NSArray *seperate = [full componentsSeparatedByString:@", "];
    Area = seperate[0];
    Country = seperate[1];
    sendDetails.Area = Area;
    sendDetails.Country = Country;
    NSLog(@"%@%@",self.longitude,self.latitude);
    NSString *lt = self.latitude;
    NSString *ln = self.longitude;
    sendDetails.longitude = lt;
    sendDetails.latitude = ln;
}

在ForecastViewController.h

//
//  ForecastViewController.h
//  Weather App
//
//  Created by Mac-Mini-2 on 10/02/16.
//  Copyright © 2016 Mac-Mini-2. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#import <Reachability.h>

@interface ForecastViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UILabel *place;
@property (weak, nonatomic) IBOutlet UITableView *table;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;

@property NSString *Area;
@property NSString *Country;
@property NSString *latitude;
@property NSString *longitude;

@end

在ForecastViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.table.delegate = self;
    self.table.dataSource = self;
    self.place.text = [NSString stringWithFormat:@"%@, %@",self.Area,self.Country];
    locationName = [NSString stringWithFormat:@"%@, %@",self.Area,self.Country];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    metric = [defaults boolForKey:@"metric"];
    Reachability *reach = [Reachability reachabilityWithHostName:@"www.google.com"];
    NSInteger x = [reach currentReachabilityStatus];
    if (x > 0)
    {
        reachable = YES;
        NSLog(@"%@, %@",self.latitude,self.longitude);
        [self getForecast:self.latitude longitudes:self.longitude];

    }
    else
    {
        reachable = NO;
        [self getData];
        errorMsg = @"Because of unavailability of Network Realtime information wont be available.";
        [self performSelectorOnMainThread:@selector(displayAlert) withObject:NULL waitUntilDone:YES];
    }
    [reach startNotifier];
}

请告诉我这里可能出错的地方。 我通过将数据记录到控制台来检查。打印区域和国家/地区,但经度和纬度为空值。

3 个答案:

答案 0 :(得分:0)

如果您正在推动ForecastViewController,那么实施prepareForSegue:sender:可能是您的最佳选择。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

    if ([[segue identifier] isEqualToString:@"viewForecast"]){

    NSArray *seperate = [full componentsSeparatedByString:@", "];
    Area = seperate[0];
    Country = seperate[1];        
        [(ForecastViewController *) [segue destinationViewController] setArea:area];
[(ForecastViewController *) [segue destinationViewController] setCountry:country];
...
...
    }
}

答案 1 :(得分:0)

我可以将纬度和经度存储为第一个控制器中的数字吗?你确定&#39; sendDetails.latitude&#39;和经度&#39;在&#39; ViewController&#39;?

中正确设置

答案 2 :(得分:0)

我认为你需要先在ForecastViewController.h中设置两个点

@property (Strong, nonatomic) NSString *Area;
@property (Strong, nonatomic) NSString *Country;
@property (Strong, nonatomic) NSString *latitude;
@property (Strong, nonatomic) NSString *longitude;

和@synthsize在ForecastViewController.m中。 其次: -

  - (IBAction)forecast:(UIButton *)sender
    {


     ForecastViewController *sendDetails = [self.storyboard instantiateViewControllerWithIdentifier:@"your ForecastViewController name in storyboard"];
    NSArray *seperate = [full componentsSeparatedByString:@", "];
    Area = seperate[0];
    Country = seperate[1];
    sendDetails.Area = Area;
    sendDetails.Country = Country;
    NSLog(@"%@%@",self.longitude,self.latitude);
    NSString *lt = self.latitude;
    NSString *ln = self.longitude;
    sendDetails.longitude = lt;
    sendDetails.latitude = ln;
    [self.navigationController pushViewController:sendDetails animated:YES];
}