Search code examples
iosobjective-cuitableviewuipageviewcontrollernsnotification

Specifying different images to use for a UIPageViewController called programatically


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!


Solution

  • 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!