0

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]];
}
}
Rajesh
  • 10,318
  • 16
  • 44
  • 64
sakthi
  • 19
  • 3
  • 1
    http://nshipster.com/nsnotification-and-nsnotificationcenter/ this is propably the easiest solution.. other solutions might be setting up a delegate or forwarding a callback block – Marc Mar 31 '14 at 05:37
  • btw I think your problem is, that you create a new parent VC in your modal VC.. but you need the original parent VC! – Marc Mar 31 '14 at 05:41

1 Answers1

0

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 i.e. '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.

Community
  • 1
  • 1
Tom Susel
  • 3,397
  • 1
  • 24
  • 25