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.
The
didSetobserver 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:
Objective-C:
It's quite similar in objective C, but you don't have a
didSetobserver, instead use a custom setter. The only difference is, since it's a setter, that you have to set your variable