Search code examples
iosobjective-cuipageviewcontroller

Create dynamically different (UIViewcontroller) instance names in for loop?


I want to create an array of ViewControllers of my class SlideViewController and loop through them to assign different variable names to it or loop through the list and dynamically assign different names to my SlideViewController with array index appending like below:

for (int i=0; i<[_jsonArray count]; i++) {
    NSDictionary *dict = _jsonArray[i];

    SlideViewController *firstViewController = [[SlideViewController alloc] initWithNibName:@"SlideView" bundle:nil];

    //i want to do like this --- SlideViewController *firstViewController[i] = [[SlideViewController alloc] initWithNibName:@"SlideView" bundle:nil]; 

    NSLog(@"weekly Question.... %@",[dict objectForKey:@"weeklyquestion"]);

    firstViewController.question.text = [dict objectForKey:@"weeklyquestion"];

    [stepsArray addObject:firstViewController];
}

JJGMPageViewController *pageViewController = [[JJGMPageViewController alloc] init];

pageViewController.delegate = self;

[pageViewController addViewControllers:stepsArray];
[self presentViewController:nav animated:YES completion:nil];

The above is not assigning the question to my firstViewController.question.text and it's overwriting same instance.


Solution

  • From what I see on this line, firstViewController.question.text = [dict objectForKey:@"weeklyquestion"];, you're either setting your UILabel or UITextView's text property. However, neither UILabel or UITextView may be initialized at that state.

    So instead, declare an NSString property in your SlideViewController and set its value in your for loop. Then in your SlideViewController's viewDidLoad method, set that NSString to your label's or textview's text property.