指定不同的图像以用于以编程方式调用的 UIPageViewController

Specifying different images to use for a UIPageViewController called programatically

我正在构建一个非常简单的应用程序,但实际上我在一个概念上遇到了一些困难。

我有一个 UITableViewController 有 15 个静态单元格,每个都有不同的语言。当我单击一种语言时,我会以模式和编程方式调用 UIPageViewController,它充当显示图像和滚动图像的方式。

只要测试一种语言,我就可以在 UIPageViewControllerviewDidLoad 中加载图像,例如:

_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];

但我想更改它,以便根据调用它的单元格显示不同的图像。

所以我在调用 UIPageViewController:

UITableViewController 中做了这个
- (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];
    }

}

然后,在 UIPageViewController 中,在 viewDidLoad 中,我调用:

    [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];

}

但是,这就是问题所在。如果我从 Notification 中调用 chooseImage 方法,我将如何确定要显示的图像?

- (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];

}

所以基本上,我想要一些东西来触发,调用 UIPageViewController 并显示适当的图像,具体取决于所选的语言。

我该怎么做?

如有任何想法,我们将不胜感激!

我建议使用变量而不是使用通知。

像这样:

@interface LeafletImageViewController : UIViewController

@property (nonatomic, copy) NSString *language;

@end

然后你必须在UITableViewController中设置language:

- (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];
}

最后可以通过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;
}

希望对您有所帮助!