iOS - Pass data from one viewcontroller to use in the init method of another viewcontroller

433 views Asked by At

I'm able to pass data between two view controllers using the prepareForSegue method. But in that way, the passed data cannot be used in the second view controller's init method.

Also, I'm using XLForm. So accessing data inside the init method is necessary.

Can anyone help me solve this problem.

Here's the first view controller's prepareForSegue method:

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

    if ([[segue identifier] isEqualToString:@"SessionToWorkout"])
    {
        WorkoutsViewController *vc = [segue destinationViewController];

        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

        UITableViewCell *selectedCell = [self.tableView cellForRowAtIndexPath:indexPath];
        cellName = selectedCell.textLabel.text;

        NSString *sectionTitle = [self tableView:self.tableView titleForHeaderInSection:indexPath.section];
        sectionName = sectionTitle;

        vc.sectionName = sectionName;
        vc.cellName = cellName;
    }

}

And here's the initWithCoder method of the second view controller:

- (instancetype)initWithCoder:(NSCoder *)coder

    {
        self = [super initWithCoder:coder];
        if (self) {
            //Retrieve Workouts from DB
            NSString *day_id;

            day_id = [[DBHandler database] getDayIdWhere:@[@"day_name"]
                                             whereValues:@[cellName]];

            workoutsArray = [[DBHandler database] getWorkoutsForDayWhere:@[@"day_id"]
                                                             whereValues:@[day_id]];

            AppLog(@"Workouts array %@", workoutsArray);

            [self initializeForm];
        }
        return self;
    }

Inside the initWithCoder method, I need to use the cellName variable's value (which has been passed to this view controller from the previous view controller) to call the database method.

Any idea or suggestions how to do that? Thanks in advance.

2

There are 2 answers

4
Michaël Azevedo On

The didSet observer is called when a variable is modified (it's not called when the variable is initialized). Initialize the database when the cellName is set :

Swift:

var cellName : String = "" {
    didSet {
     // The cell name has been set, create the database here as in any other function
    }
}

Objective-C:

It's quite similar in objective C, but you don't have a didSet observer, instead use a custom setter. The only difference is, since it's a setter, that you have to set your variable

@property(nonatomic, strong) NSString * cellName;

-(void)setCellName:(NSString *)newValue
{
    // First, set the new value to the variable
    _cellName = newValue;

    // The cell name has been set, create the database here 
}
0
Ahmed Abdallah On
if ([[segue identifier] isEqualToString:@"SessionToWorkout"])
{
    UITableViewCell *cell = sender;
    // Get reference to the destination view controller
    WorkoutsViewController *vc = [segue destinationViewController];
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    //you can get cell value in didSelectRowAtIndexPath fun
    cellName = cell.textLabel.text;
    NSString *sectionTitle = [self tableView:self.tableView titleForHeaderInSection:indexPath.section];
    sectionName = sectionTitle;
    [vc setSectionName: sectionName];
    [vc setCellName: cellName];
    //check it have right value
    NSLog(@"Cell name %@ Section name %@", cellName ,sectionName);
    //*** in viewControllerB set sectionName, cellName as @property and set it to @synthesize 
}