2016-02-24 115 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]; 
} 
相關問題