Question

I am a newbie of IOS development, my problem is I am using UIScrollView and UIPageControl to show guide info to users. I created a xib file related to GuideViewController via setting up File's Owner. I setup the content size and statically add subviews in xib files. But I encountered some strange thing, scrollview only shows the first subview on screen, all following display is just white background (when I scrolling). Following is the code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSInteger numberOfPages = 2;
    self.pageControl.numberOfPages = numberOfPages;
    self.pageControl.currentPage = 0;

    self.pageScroll.delegate = self;
    self.pageScroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfPages,
                                        self.view.frame.size.height);
    self.pageControl.currentPageIndicatorTintColor = [UIColor redColor];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat pageWidth = self.view.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth/2) / pageWidth) + 1;
    self.pageControl.currentPage = page;
}

I googled a lot to find the answer but still stuck here. I am also confused by this sentence "When a user taps a page control to move to the next or previous page, the control sends the UIControlEventValueChanged event for handling by the delegate" in xcode help. It seems UIPageControl has no delegate attribute,so how I can capture the UIControlEventValueChanged event.

Actually I am developing my main UI on storyboard, but I kind of want separate UI design to make more space for storyboard, so i use xib file for some other part UI, I do not know whether this will cause some problems. Thanks

Was it helpful?

Solution

My solution was this:

Add a IBAction in your header:

 - (IBAction)changePage;

Dont forget to set the Outlet and the property for the pageControl:

IBOutlet UIPageControl *pageControl;
@property (nonatomic, retain) IBOutlet UIPageControl* pageControl;

Add a BOOLEAN that the pagecontrol updates:

BOOL pageControlBeingUsed;

Here is my full header:

@interface ViewC : UIViewController <UIScrollViewDelegate> {

    IBOutlet UIScrollView *scrollView;
    IBOutlet UIPageControl *pageControl;

    BOOL pageControlBeingUsed;
}

@property (nonatomic, retain) IBOutlet UIScrollView* scrollView;
@property (nonatomic, retain) IBOutlet UIPageControl* pageControl;

- (IBAction)changePage;

@end

(Don't forget to synthesize the propertys) The second step you did already with the viewdidLoad method but you didn't connected the Boolean to the method! Try this:

- (void)scrollViewDidScroll:(UIScrollView *)sender {
    if (!pageControlBeingUsed) {
        // Switch the indicator when more than 50% of the previous/next page is visible
        CGFloat pageWidth = self.scrollView.frame.size.width;
        int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
        self.pageControl.currentPage = page;
    }
}

Add at the end this code for the IBAction and that when the pageControl was pressed at a button the site changes.

- (IBAction)changePage {
    // update the scroll view to the appropriate page
    CGRect frame;
    frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
    frame.origin.y = 0;
    frame.size = self.scrollView.frame.size;
    [self.scrollView scrollRectToVisible:frame animated:YES];

    pageControlBeingUsed = YES;

}

IMPORTANT: Connect the IBAction with "value changed" option in the xib. and set the delegate of the Controller to itself:

enter image description here

And then it should be like this: enter image description here

That is all! Hope you fixed your issue. I would be glad if you give my awnser the "tick" if it helped you!

Greetings,

Noah

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