0

I am building up a pretty straight forward app, but I'm having a bit of difficulty actually with one concept.

I have a UITableViewController with 15 static cells, each with a different language. When I click on a language, I am modally and programatically calling a UIPageViewController which acts as a means to display the image and scroll through them.

With just one language to test, I can load the images in the viewDidLoad of the UIPageViewController, like:

_modelArray = [NSMutableArray arrayWithObjects:[[ImageModel alloc] initWithImageName:@"3FactsEnglishPage1.png"], [[ImageModel alloc] initWithImageName:@"3FactsEnglishPage2.png"], [[ImageModel alloc] initWithImageName:@"3FactsEnglishPage3.png"], [[ImageModel alloc] initWithImageName:@"3FactsEnglishPage4.png"], [[ImageModel alloc] initWithImageName:@"3FactsEnglishPage5"], nil];

_pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll                                                          navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
                                                                    options:nil];

_pageViewController.delegate = self;
_pageViewController.dataSource = self;

WalkthroughViewController *imageViewController = [[WalkthroughViewController alloc] init];
imageViewController.model = [_modelArray objectAtIndex:0];
NSArray *viewControllers = [NSArray arrayWithObject:imageViewController];

But I want to change that so that depending on which cell called it, it will display different images.

So I did this in the UITableViewController that calls the UIPageViewController:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self.threeFactsTableView cellForRowAtIndexPath:indexPath];
    if ([cell.textLabel.text isEqualToString:@"English"])
    {
        LeafletImageViewController *tutorial = [[LeafletImageViewController alloc] init];
        [self presentViewController:tutorial animated:YES completion:nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ThreeFactsEnglish" object:self userInfo:nil];
    }
    if ([cell.textLabel.text isEqualToString:@"Chinese"])
    {
        LeafletImageViewController *tutorial = [[LeafletImageViewController alloc] init];
        [self presentViewController:tutorial animated:YES completion:nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ThreeFactsChinese" object:self userInfo:nil];
    }

}

Then, in the UIPageViewController, in the viewDidLoad, I call:

    [self imageNotificationListeners];

- (void)imageNotificationListeners
{
    NSLog(@"The imageNotificationListeners is being called");
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(chooseImage)
                                                 name:@"ThreeFactsEnglish" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(chooseImage)
                                                 name:@"ThreeFactsChinese" object:nil];

}

But then, this is where the issue is. If I call the chooseImage method from the Notification, how will I determine which image to show?

- (void)chooseImage
{
    NSLog(@"The chooseImage is being called");
    _modelArray = [NSMutableArray arrayWithObjects:[[ImageModel alloc] initWithImageName:@"3FactsEnglishPage1.png"], [[ImageModel alloc] initWithImageName:@"3FactsEnglishPage2.png"], [[ImageModel alloc] initWithImageName:@"3FactsEnglishPage3.png"], [[ImageModel alloc] initWithImageName:@"3FactsEnglishPage4.png"], [[ImageModel alloc] initWithImageName:@"3FactsEnglishPage5"], nil];

}

So essentially, I want something to trigger, to call the UIPageViewController and to display the appropriate images, depending on the language that's been chosen.

How would I go about doing this?

Any thoughts are appreciated!

amitsbajaj
  • 1,304
  • 1
  • 24
  • 59

1 Answers1

2

I'd suggest to use a variable instead of using notification.

Like this:

@interface LeafletImageViewController : UIViewController

@property (nonatomic, copy) NSString *language;

@end

Then you have to set the language in the UITableViewController:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self.threeFactsTableView cellForRowAtIndexPath:indexPath];

    LeafletImageViewController *tutorial = [[LeafletImageViewController alloc] init];
    tutorial.language = cell.textLabel.text;
    [self presentViewController:tutorial animated:YES completion:nil];
}

Finally, you can load the images through the language.

- (void)chooseImage
{
    NSLog(@"The chooseImage is being called");

    NSMutableArray *imageModels = [NSMutableArray array];
    for (int i = 1; i <= 5; i++) {
        ImageModel *imageModel = [[ImageModel alloc] initWithImageName:@"3Facts%@Page1.png", self.language];
        [imageModels addObject:imageModel];
    }
    _modelArray = imageModels;
}

Hope this will help you!

Bannings
  • 10,376
  • 7
  • 44
  • 54
  • Thanks so much @Bannings ; that is extremely helpful and I will certainly try that now. One question though, because I am calling this LeafletImageViewController from different UITableViewControllers and when this works, it'll be from other areas as well, I can't always predict that the number of images will be 5. Sometimes it could be 5, sometimes it could be a 8 or 10. Would that change the code in anyway? – amitsbajaj Jul 05 '15 at 10:55
  • You can copy the images into app bundle with `Create folder reference` and name it through each language, then you can load them with `[[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory: languageDirectory];` – Bannings Jul 05 '15 at 11:06
  • Thanks Bannings - that's really helpful. What's interesting is that I was able to get this working by pre-loading the images in the didSelect method of the UITableViewController, so that works, but your answer has been extremely helpful because it helps me achieve other similar factors that I'll be using. Thanks so much Bannings. – amitsbajaj Jul 05 '15 at 11:07