Question

I have problem with customizing each page using pagecontrol and UIScrollView. I'm customizing Page Control from Apple.

Basically I would like to have each page different with text and image alternately on different page. Page 1 will have all text, Page 2 will have just images, Page 3 will have all text and goes on.

This is original code:

    // Set the label and background color when the view has finished loading.
    - (void)viewDidLoad {
        pageNumberLabel.text = [NSString stringWithFormat:@"Page %d", pageNumber + 1];
        self.view.backgroundColor = [MyViewController pageControlColorWithIndex:pageNumber];
}

As you can see, this code shows only Page 1, Page 2 etc as you scroll right.

I tried to put in this new code but that didn't make any difference. There's no error. I know this is pretty simple code. I don't why it doesn't work.

I declare pageText as UILabel.

// Set the label and background color when the view has finished loading.
- (void)viewDidLoad {
    pageNumberLabel.text = [NSString stringWithFormat:@"Page %d", pageNumber + 1];
    self.view.backgroundColor = [MyViewController pageControlColorWithIndex:pageNumber];
    if (pageNumber == 1) {
        pageText.text = @"Text in page 1";
    }
    if (pageNumber == 2) {
            pageText.text = @"Image in page 2";
    }
    if (pageNumber == 3) {
            pageText.text = @"Text in page 3";
    }

}

I don't know why it doesn't work. Also if you have better way to do it, let me know. Thanks.

Was it helpful?

Solution

I downloaded the sample, added the following lines to the MyViewController.h

IBOutlet UILabel *pageText;

@property (nonatomic, retain) UILabel *pageText;

In the MyViewController.m I added the @synthesize pageText; line and your viewDidLoad contents.

I opened the MyView.xib/en file in interface builder and connected the new label. Compiled and ran and it worked for the most part. Only thing that didn't seem right was that it was a page behind. so I rewrote it to just handle odd or even pages appropriately.

- (void)viewDidLoad {
    pageNumberLabel.text = [NSString stringWithFormat:@"Page %d", pageNumber + 1];
    self.view.backgroundColor = [MyViewController pageControlColorWithIndex:pageNumber];
    if (pageNumber % 2) {
        pageText.text = [NSString stringWithFormat:@"Image in page %d", pageNumber + 1];
    } else {
        pageText.text = [NSString stringWithFormat:@"Text in page %d", pageNumber +1];
    }   
}

OTHER TIPS

This answer will have very little code, as I have no distinct example. You basically want to have some array of pages, which holds a view (or view controller) for each page. There is no necessity that each view/controller in the array be the same (though you should have it be all views or all view controllers, not both). Once you have that, you can load something distinct depending on which page you're loading.

Does that make sense?

try to download this project from SVN repo. I've been unable to finish the documentation but check out IGUIScrollViewElements ... there is a short example for IGUIScrollViewImage and the code is quite similar ... I'll do my best to finish examples for the remaining ones asap ...

http://code.google.com/p/iphone-guru-uilibrary/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top