0

我是新手尝试创建一个仅使用单视图应用程序来适应新 xcode 的基于页面的应用程序的图书应用程序,因为学习模板非常混乱。

我找到了这段代码,我试图将这项工作的 HTML 内容替换为 UIImageView,但我失败了。

- (void) createContentPages
{
    NSMutableArray *pageStrings = [[NSMutableArray alloc] init];
    for (int i = 1; i < 11; i++)
    {
        NSString *contentString = [[NSString alloc]
                                   initWithFormat:@"<html><head></head><body><h1>Chapter %d</h1><p>This is the page %d of content displayed using UIPageViewController in iOS 5.</p></body></html>", i, i];
        [pageStrings addObject:contentString];
    }
    pageContent = [[NSArray alloc] initWithArray:pageStrings];
}

我想要的是每页显示不同的图像。

这就是我所做的。(我知道这是错误的,我只是尝试过)

- (void) createContentPages
{
    NSMutableArray *pageStrings = [[NSMutableArray alloc] init];
    for (int i = 1; i < 11; i++)
    {
        NSArray *photos = [[NSArray arrayWithObjects:   
                        [UIImage imageNamed:@"p1.png"],
                        [UIImage imageNamed:@"p2.png"],
                        [UIImage imageNamed:@"p3.png"],
                        [UIImage imageNamed:@"p4.png"],
                        nil];
        [pageStrings addObject:photos];
    }
    pageContent = [[NSArray alloc] initWithArray:pageStrings];
}

它在每个页面中都有 UIWebView 。如果我用 UIImageView 替换它,它会运行吗?

这是其余的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self createContentPages];
    NSDictionary *options = 
    [NSDictionary dictionaryWithObject:
     [NSNumber numberWithInteger:UIPageViewControllerSpineLocationMin]
                                forKey: UIPageViewControllerOptionSpineLocationKey];

    self.pageController = [[UIPageViewController alloc] 
                           initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl
                           navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
                           options: options];

    pageController.dataSource = self;
    [[pageController view] setFrame:[[self view] bounds]];

    contentViewController *initialViewController = 
    [self viewControllerAtIndex:0];
    NSArray *viewControllers =  
    [NSArray arrayWithObject:initialViewController];

    [pageController setViewControllers:viewControllers  
                             direction:UIPageViewControllerNavigationDirectionForward 
                              animated:NO 
                            completion:nil];

    [self addChildViewController:pageController];
    [[self view] addSubview:[pageController view]];
    [pageController didMoveToParentViewController:self];
}

- (contentViewController *)viewControllerAtIndex:(NSUInteger)index
{
    // Return the data view controller for the given index.
    if (([self.pageContent count] == 0) || 
        (index >= [self.pageContent count])) {
        return nil;
    }

    // Create a new view controller and pass suitable data.
    contentViewController *dataViewController = 
    [[contentViewController alloc]
     initWithNibName:@"contentViewController" 
     bundle:nil];
    dataViewController.dataObject = 
    [self.pageContent objectAtIndex:index];
    return dataViewController;
}

- (NSUInteger)indexOfViewController:(contentViewController *)viewController
{
    return [self.pageContent indexOfObject:viewController.dataObject];
}

- (UIViewController *)pageViewController:
(UIPageViewController *)pageViewController viewControllerBeforeViewController:
(UIViewController *)viewController
{
    NSUInteger index = [self indexOfViewController:
                        (contentViewController *)viewController];
    if ((index == 0) || (index == NSNotFound)) {
        return nil;
    }

    index--;
    return [self viewControllerAtIndex:index];
}

- (UIViewController *)pageViewController:
(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    NSUInteger index = [self indexOfViewController:
                        (contentViewController *)viewController];
    if (index == NSNotFound) {
        return nil;
    }

    index++;
    if (index == [self.pageContent count]) {
        return nil;
    }
    return [self viewControllerAtIndex:index];
}

在子类上

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [webView loadHTMLString:dataObject 
                    baseURL:[NSURL URLWithString:@""]];
}
4

2 回答 2

0

将您的 createContentPages() 方法与这一小改动一起使用(仅 UIImage 并不代表您可以放在“视图”上的任何内容,而是您需要使用包含 UIImages 的 UIImageView):

- (void) createContentPages
{
    NSMutableArray *pageStrings = [[NSMutableArray alloc] init];
    for (int i = 1; i < 11; i++)
    {
        NSArray *photos = [[NSArray arrayWithObjects:   
                        [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"p1.png"] autorelease],
                        [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"p2.png"] autorelease],
                        [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"p3.png"] autorelease],
                        [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"p4.png"] autorelease],
                        nil];
        [pageStrings addObject:photos];
    }
    pageContent = [[NSArray alloc] initWithArray:pageStrings];
}

您可以使用主控制器中的代码(您发布的大块代码)。

您需要做的唯一更改是您所谓的“子类”。(实际上是 UIPageViewController 处理显示实际页面)。

在那个 UIPageViewController 中删除“webView”(UIWebView)。这可能是在 .xib 文件中完成的(看起来不像是在代码中添加的)。然后让 UIPageViewController 中的 viewWillAppear 看起来像这样:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    self.view = dataObject;

}

这应该够了吧。基本上,我们不再使用 UIWebView 来显示 HTML 数据,而只是添加 dataObject(一个 UIImageView)作为 UIPageViewController 的子视图。希望这可以帮助!

于 2012-04-26T03:15:23.770 回答
0

任何时候你都可以使用 HTML 标签来使用 HTML 页面中的图片<IMG>。所以,没有理由使用 UIImageView。

于 2012-05-12T02:40:03.940 回答