2016-02-24 118 views
0

这里是我传递数据的按钮函数。将数据传递给视图控制器

问题是区域和国家的详细信息传递给下一个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]; 
} 

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

+1

您没有向我们展示您设置经度和纬度的位置/方式。 – jcaron

+0

我正在打印他们,看他们是否包含数据...并确保我没有通过零值.. ,他们不..我也检查了值... 正如我所说的区域和国家得到通过但其他两个不要...我很困惑 – Shravan

+0

我编辑了我的问题,你可以看到我在哪里发送它们 – Shravan

回答

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]; 
... 
... 
    } 
} 
+0

任何想法如何只有地区和国家工作,其他两个不是? – Shravan

+0

那些失踪,'[(ForecastViewController *)[segue destinationViewController] setLongitude:lt]; [(ForecastViewController *)[segue destinationViewController] setLatitude:ln];' – Lucho

+0

您还必须在故事板上添加segue – Lucho

0

我可以把那个纬度和经度作为数字存储在第一个控制器吗?你确定在'ViewController'中'sendDetails.latitude'和'longitude'设置正确吗?

+0

我已经将lat和long转换为字符串,然后通过它们,即使这似乎并不奏效。 yes ..否则NSLog wouldnt giveme结果它打印所有细节区域国家拉特和长我的ViewController.h,但在ForecastViewController它只打印区域和国家 – Shravan

0

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

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

和ForecastViewController.m @synthsize它。 其次: -

- (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]; 
} 
相关问题