UIControlEventValueChanged event is not triggered for the UIPageControl consistently
Asked Answered
C

2

5

Lets assume we have ten pages.

An event handler for the event is added as below:

Run the app. Now in the ten pages, by default page 1 (index 0) is selected. Touch the second page or third page. The event wont be triggered. If the last page is selected, the event will be triggered. The same happens with last page. Once you have selected the last page, select the previous page. The event will not be triggered, but if you select the first page, the event will not be triggered.

To see an easy demonstration of this case, download the UICatalog example and open the ControlsViewController.m and change the UIControlEventTouchUpInside to UIControlEventValueChanged in line no 375.

- (UIPageControl *)pageControl
{
    if (pageControl == nil) 
    {
        CGRect frame = CGRectMake(120.0, 14.0, 178.0, 20.0);
        pageControl = [[UIPageControl alloc] initWithFrame:frame];
        [pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];

        // in case the parent view draws with a custom color or gradient, use a transparent color
        pageControl.backgroundColor = [UIColor grayColor];

        pageControl.numberOfPages = 10; // must be set or control won't draw
        pageControl.currentPage = 0;
        pageControl.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
    }
    return pageControl;
}
Cropdusting answered 22/11, 2012 at 7:46 Comment(0)
P
10

You might be misunderstanding how the page control works. It is a visual indicator of the number of pages present, but tapping on a particular dot doesn't go to that page. You only ever move one page at a time, tapping on the left half moves back a page, and on the right half moves forward a page.

If you're on the first page, and you tap on the left half, you can't move back another page, so nothing happens.

I don't really like that behaviour, particularly on the iPad, so usually use a subclass or my own touch handling to work out if the touch location is to the left or right of the currently selected page, and send the event appropriately.

Passional answered 22/11, 2012 at 7:53 Comment(1)
@jrtuton: Thanks . When I tested the app in iPad I was able to understand the case better.Cropdusting
N
0

You can use UITapGestureRecognizer instead of using addTarget:self action:@selector()

//Added UITapGestureRecognizer instead of using addTarget: method        
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] init];         
tapGesture addTarget:self action:@selector(pageAction:) ];
[pageControl addGestureRecognizer:tapGesture];
[tapGesture release];
tapGesture = nil;


-(void)pageAction:(UITapGestureRecognizer *)tapGesture{  

   UIPageControl *pageControl = (UIPageControl *)tapGesture.view;      

   NSLog(@"page Number: %d",pageControl.currentPage);


}
Nurserymaid answered 22/11, 2012 at 8:16 Comment(1)
If you want to detect taps, it's much simpler to just use the built in tap handler that all UIControls have. For example: [pageControl addTarget:self action:@selector(handleTap:) forControlEvents:UIControlEventTouchUpInside]Pachydermatous

© 2022 - 2024 — McMap. All rights reserved.