Ios 在视图控制器之间传递数据时,可以';我不明白为什么这个参数是空的

Ios 在视图控制器之间传递数据时,可以';我不明白为什么这个参数是空的,ios,objective-c,Ios,Objective C,我正在将一个对象从滑动菜单ViewController(源)传递到HomeViewController(目标) 在我的MenuViewController上,用户输入机场代码,选择它,主视图控制器对其执行一些操作我的问题是,对象以null的形式到达HomeViewController,我不知道为什么 我使用NSString属性airportID启动NSObject FPLAirport的w/a子类 在MenuViewController中,我使用AddAirportViewController通

我正在将一个对象从滑动菜单ViewController(源)传递到HomeViewController(目标)

在我的MenuViewController上,用户输入机场代码,选择它,主视图控制器对其执行一些操作我的问题是,对象以null的形式到达HomeViewController,我不知道为什么

我使用NSString属性airportID启动NSObject FPLAirport的w/a子类

在MenuViewController中,我使用AddAirportViewController通过模式序列添加表视图单元格

当用户点击MenuViewController中填充的表视图单元格时,MenuViewController(源)将字符串airportID传递给homeViewController(目标),我在那里调用一个方法来做一些工作

我的airportID对象为空,我无法找出原因

以下是相关代码:

首先,在我的AddAirportViewController.m中,prepareForSegue方法:

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if (sender != self.doneButton) return;
    if (self.airportTextField.text.length > 0) {
        self.airport = [[FPLAirport alloc] init];
        self.airport.airportID = self.airportTextField.text;
    }
接下来是MenuTableViewController.m的prepareForSegue和unwind方法:

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

    if ( [segue isKindOfClass: [SWRevealViewControllerSegue class]] ) {
        SWRevealViewControllerSegue* rvcs = (SWRevealViewControllerSegue*) segue;

        SWRevealViewController* rvc = self.revealViewController;

        rvcs.performBlock = ^(SWRevealViewControllerSegue* rvc_segue, UIViewController* svc, UIViewController* dvc)
        {
            UINavigationController* nc = [[UINavigationController alloc] initWithRootViewController:dvc];
            [rvc pushFrontViewController:nc animated:YES];
        };
    }

    //pass the selected airport to the home view controller

    if ([[segue identifier] isEqualToString:@"sw_selectedAirport"]) {
        FPLHomeTableViewController *homeViewController = [segue destinationViewController];
        homeViewController.airportID = [self.airportsArray objectAtIndex:[self.tableView indexPathForSelectedRow].row];
    }
}

-(IBAction)unwindToMenu:(UIStoryboardSegue *)segue {
    FPLAddAirportViewController *source = [segue sourceViewController];
    FPLAirport *addedAirport = source.airport;
    if (addedAirport != nil) {
        [self.airportsArray addObject:addedAirport];
        [self.tableView reloadData];
    }
My HomeTableViewController.m viewDidLoad:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.revealButtonItem setTarget: self.revealViewController];
    [self.revealButtonItem setAction: @selector( revealToggle: )];
    [self.navigationController.navigationBar addGestureRecognizer: self.revealViewController.panGestureRecognizer];

    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    self.navigationController.navigationBar.translucent = YES;

    [self configureView];
    [self initializeWeatherData];
}
和初始化AtherData方法:

-(void) initializeWeatherData {

        //test for an existing airport ID in airportsArray

    NSLog(@"self.airportID is %@", self.airportID);

    if (self.airportID != nil) {

        FPLWeatherData *weatherData = [[FPLWeatherData alloc] init];

        //        [weatherData downloadHomeFieldObservation];
        [weatherData downloadWeatherObservationWithID:self.airportID];

        self.airportNameLabel.text = weatherData.observationAirport;
        self.timeAgoLabel.text = weatherData.timeSinceObservation;
        self.windLabel.text = weatherData.windObservation;
        self.altimeterLabel.text = weatherData.altimeterObservation;
        self.visibilityLabel.text = weatherData.visibilityObservation;

    }
最后,NSDictionary子类FPLWeatherData类的相关部分:

@implementation FPLWeatherData

-(void)downloadWeatherObservationWithID:(NSString *)airportID {

    //call the API
    //airport location hard-coded

    NSURL *weatherURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://api.wunderground.com/api/APIKeyGoesHere/geolookup/astronomy/conditions/q/%@.json", airportID]];
    NSData *weatherData = [NSData dataWithContentsOfURL:weatherURL];
    NSError *error  = nil;
    NSDictionary *weatherDictionary = [NSJSONSerialization JSONObjectWithData:weatherData options:0 error:&error];

    //observation Airport;
    self.observationAirport = [[weatherDictionary valueForKey:@"current_observation"] valueForKey:@"station_id"];

    //wind observation
    NSString *windDirection = [[weatherDictionary valueForKey:@"current_observation"] valueForKey:@"wind_degrees"];

    //    NSString *windIsNorth = @"0"; fix this!!

    //    if ([windDirection isEqualToString:windIsNorth]) {
    //        windDirection = @"360";
    //    }

    NSString *windSpeed =[[weatherDictionary valueForKey:@"current_observation"] valueForKey:@"wind_mph"]; //convert to knots
    self.windObservation = [NSString stringWithFormat:@"%@ / %@", windDirection, windSpeed];

    //visibility, altimeter, condition

    self.visibilityObservation = [[weatherDictionary valueForKey:@"current_observation"] valueForKey:@"visibility_mi"];
    self.altimeterObservation = [[weatherDictionary valueForKey:@"current_observation"] valueForKey:@"pressure_in"];
    self.weatherCondition = [[weatherDictionary valueForKey:@"current_observation"] valueForKey:@"weather"];

    //time since observation - cache this date formatter later

    NSString *observationTime = [[weatherDictionary valueForKey:@"current_observation"] valueForKey:@"observation_time_rfc822"];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss ZZZZ"];
    NSDate *timeNow = [NSDate date];
    NSDate *timeObserved = [dateFormatter dateFromString:observationTime];
    int observationDifference = [timeNow timeIntervalSinceDate:timeObserved] / 60;
    self.timeSinceObservation = [NSString stringWithFormat:@"%i minutes ago", observationDifference];


    //compose sunset time from API

    NSString *sunsetHour = [[[weatherDictionary valueForKey:@"sun_phase"] valueForKey:@"sunset"] valueForKey:@"hour"];
    int sunsetHourInt = [sunsetHour intValue];

    NSString *sunsetMinute = [[[weatherDictionary valueForKey:@"sun_phase"] valueForKey:@"sunset"] valueForKey:@"minute"];
    int sunsetMinuteInt = [sunsetMinute intValue];

    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSTimeZoneCalendarUnit fromDate:[NSDate date]];
    [components setHour:sunsetHourInt];
    [components setMinute:sunsetMinuteInt];

    NSDate *sunsetTime = [gregorian dateFromComponents:components];
    int sunsetDifference = [sunsetTime timeIntervalSinceDate:timeNow] / 60;

    if (sunsetDifference <= 0) {
        self.isNight = YES;
    } else {
        self.isNight = NO;
    }
}
@实现FPLWeatherData
-(void)下载WeatherObservationWithID:(NSString*)airportID{
//调用API
//机场位置硬编码
NSURL*weatherURL=[NSURL URLWithString:[NSString stringWithFormat:@]http://api.wunderground.com/api/APIKeyGoesHere/geolookup/astronomy/conditions/q/%@.json“,airportID]];
NSData*weatherData=[NSData dataWithContentsOfURL:weatherURL];
n错误*错误=nil;
NSDictionary*weatherDictionary=[NSJSONSerialization JSONObjectWithData:weatherData选项:0错误:&错误];
//观察机场;
self.observationAirport=[[weatherDictionary valueForKey:@“当前观测”]valueForKey:@“站点id”];
//风观测
NSString*风向=[[weatherDictionary valueForKey:@“当前观测”]valueForKey:@“风度”];
//NSString*windIsNorth=@“0”;修复此问题!!
//if([windDirection isEqualToString:windIsNorth]){
//风向=@“360”;
//    }
NSString*windSpeed=[[weatherDictionary valueForKey:@“current_observation”]valueForKey:@“wind_mph”];//转换为节
self.windObservation=[NSString stringWithFormat:@“%@/%@”,风向,风速];
//能见度、高度表、状况
self.visibilityObservation=[[weatherDictionary valueForKey:@“当前观察”]valueForKey:@“可见性”];
self.altimeterObservation=[[weatherDictionary valueForKey:@“当前观测”]valueForKey:@“压力输入”];
self.weatherCondition=[[weatherDictionary valueForKey:@“当前观测”]valueForKey:@“天气”];
//观察后的时间-稍后缓存此日期格式化程序
NSString*observationTime=[[weatherDictionary valueForKey:@“当前观察”]valueForKey:@“观察时间”rfc822];
NSDateFormatter*dateFormatter=[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@“EEE,dd-MMM-yyyy-HH:mm:ss-ZZZZ”];
NSDate*timeNow=[NSDate-date];
NSDate*timeObserved=[dateFormatter dateFromString:observationTime];
int observationDifference=[timeNow timeIntervalSinceDate:timeObserved]/60;
self.timeSinceObservation=[NSString stringWithFormat:@“%i分钟前”,observationDifference];
//从API编写日落时间
NSString*sunsetHour=[[weatherDictionary valueForKey:@“sun_phase”]valueForKey:@“日落”]valueForKey:@“小时”];
int sunsetHour=[sunsetHour intValue];
NSString*sunsetMinute=[[weatherDictionary valueForKey:@“sun_phase”]valueForKey:@“sunset”]valueForKey:@“minute”];
int sunsetMinuteInt=[sunsetminuteintvalue];
NSCalendar*gregorian=[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents*components=[gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSTimeZoneCalendarUnit fromDate:[NSDate date]];
[组件设置时间:sunsetHourInt];
[组件设置分钟:太阳设置分钟];
NSDate*sunsetTime=[gregorian dateFromComponents:components];
int sunsetDifference=[sunsetTime-timeIntervalSinceDate:timeNow]/60;

如果(sunsetDifference这个问题的建议答案将我引向了一个我从未考虑过的方向

事实证明,这不是一个实例化问题,也不是hotlicks建议的问题

我试图直接访问对象,而不是对象的string属性。我这样做是为了纠正问题:在MenuTableViewController.m的prepareForSegue方法中,我更改了以下代码:

 //pass the selected airport to the home view controller

    if ([[segue identifier] isEqualToString:@"sw_selectedAirport"]) {
        FPLHomeTableViewController *homeViewController = [segue destinationViewController];
        homeViewController.airportID = [self.airportsArray objectAtIndex:[self.tableView indexPathForSelectedRow].row];
    }
为此:

//pass the selected airport to the home view controller

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

    NSLog(@"the airports array is, %@", self.airportsArray);

    FPLHomeTableViewController *homeViewController = [segue destinationViewController];
    FPLAirport *selectedAirport = [self.airportsArray objectAtIndex:[self.tableView indexPathForSelectedRow].row];

    homeViewController.airportID = selectedAirport.airportID;
}

感谢您的回答,如果没有这些建议,我就不会发现问题。

通常最好将代码与您的问题、课程的相关部分一起发布。更可能的是,您不会在
准备过程中从
segue.destinationViewController
获取VC>方法。如果您正在实例化
HomeViewController
的新实例,那么您将信息传递给了错误的对象。更正了原始帖子的问题。很抱歉,像最初提出的那样,将社区的时间浪费在了一个无用的问题上。我希望这样更好。视图控制器之间传递的参数为零是不言而喻的。大约60%的时间是由于在不同的对象实例中设置了值,而您随后试图从中获取该值。30%的时间是由于在存储该值的对象实际上不存在时设置/获取了一个值--一个零指针。是的,我明白了,谢谢!我正在尝试在代码中找到我需要的位置“我在做,我在修!”!