简体   繁体   中英

Values not returning to parent view controller?

From my parent view controller I am presenting a modal viewcontroller , from the modalview user can selects his country,city,latitude and longitude etc., and the values are displayed in the next modal view , if user clicks done button in the modal view I need to update the labels in the parent view controller after doing some calculation, but the values are not updating my labels, here are the codes,

//done method in modal view controller

- (IBAction)doneButtonClicked:(id)sender {
NSLog(@"Before Done %@ %@ %f %f %f %f",_finalCountryName,_finalCityName,_finalCityLatitude,_finalCityLongitude,_finalCityTimezone,_finalCityDaylight);

//opening screen is my parent view controller
OpeningScreen *open = [[OpeningScreen alloc]init];
[open rebackInitialisation];
[open manualSalatCalculation:_finalCityLatitude manLong:_finalCityLongitude manTz:_finalCityTimezone manDls:_finalCityDaylight];
[self dismissViewControllerAnimated:YES completion:nil];
}

//method in parent view controller (OpeningScreen)

-(void)manualSalatCalculation:(double)manLat manLong:(double)manLon manTz:(double)manTz manDls:(double)manDls {
NSLog(@"Manual Values are %f %f %f %f",manLat,manLon,manTz,manDls);   ========>>> Here values are coming

if (!manLat == 0.0 && !manLon == 0.0) {
    double tZone =0.0;
    if([curDayLightStatString isEqualToString:@"Disable"]) {
        tZone = manTz;
    }
    if([curDayLightStatString isEqualToString:@"Enable"]) {
        tZone = manTz + manDls;
    }

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
self.manualTimesArray = [prayCalc getPrayerTimes:components andLatitude:manLat andLongitude:manLon andtimeZone:tZone];
NSLog(@"manual calculated array values are %@",self.manualTimesArray); =======================>>> Here values are not coming so not updating my labels
first.text = [NSString stringWithFormat:@"%@",[self.manualTimes objectAtIndex:0]];
second.text = [NSString stringWithFormat:@"%@",[self.manualTimes objectAtIndex:2]];
third.text = [NSString stringWithFormat:@"%@",[self.manualTimes objectAtIndex:3]];
fourth.text = [NSString stringWithFormat:@"%@",[self.manualTimes objectAtIndex:5]];
fifth.text = [NSString stringWithFormat:@"%@",[self.manualTimes objectAtIndex:6]];
}
}

The Problems

  1. You're calling the wrong instance of OpeningScreen . By calling [[OpeningScreen alloc] init] you are creating a new instance that is different than the parent view controller.

  2. There is a tight coupling between the modal view controller and the OpeningScreen class.

Solution

The conventional way to communicate between a modal view controller and it's parent view controller is by utilizing the 'delegate' design pattern.

When implementing it you define a protocol that the parent view controller must implement ie 'CalculatorDelegate`, Then during the creation of the modal view controller you supply the parent view controller as a weak property to the modal view controller.

This way, the child view controller does not create a new instance of OpeningScreen and doesn't even need to be aware of it's existence.

Example

See this SO post .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM