gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 21:43:21 26 4
gpt4 key购买 nike

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

我有一个包含 15 个静态单元格的 UITableViewController,每个单元格都有不同的语言。当我单击一种语言时,我以模式和编程方式调用 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];

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

所以我在调用 UIPageViewControllerUITableViewController 中做了这个:

- (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 中设置语言:

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

最后,您可以通过语言加载图片。

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

希望对您有所帮助!

关于ios - 指定不同的图像以用于以编程方式调用的 UIPageViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31229058/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com