0

I'm about to create an App.

It should have 3 main pages. So I thought of achieving this with a PageControl.

I created 3 Views and now i am stuck on the implementation of this PageControl.

Has anyone a good tutorial or a example code where i take can look at (it can be german too)?

Thanks, Michael

daris mathew
  • 429
  • 5
  • 18
Michael Gajda
  • 105
  • 1
  • 2
  • 11

1 Answers1

12

Here's a simple idea of how to use it.

PageController.h:

#import <UIKit/UIKit.h>

@interface PageController : UIViewController {
    NSArray * views;
    UIPageControl *pageControl;
}

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

- (IBAction) changePage:(id)sender;
- (void) animateToView:(UIView *)newView;

@end

PageController.m:

#import "PageController.h"

@implementation PageController

- (void)viewDidLoad {
    [super viewDidLoad];
    pageControl.numberOfPages = [views count];
    pageControl.currentPage = 0;

    // Either wire this up in Interface Builder or do it here.
    [pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
}

- (IBAction) changePage:(id)sender {
    UIView * newView = [views objectAtIndex:[pageControl currentPage]];
    [self animateToView:newView];
}

- (void) animateToView:(UIView *)newView {
     // You'd have to implement this yourself
}

@end
Cory Kilger
  • 13,034
  • 3
  • 33
  • 24
  • In PageController.h Correction: @property (nonatomic,retain) IBOutlet UIPageControl *pageControl; – Parth Bhatt Dec 29 '10 at 07:02
  • Im having problems switching views, here is my code: `#import "ViewController.h" @implementation ViewController @synthesize pageControl; - (void)viewDidLoad { [super viewDidLoad]; //Create UIViews UIView *view1 = [[UIView alloc] init]; UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image1.jpg"]]; [view1 addSubview:imageView]; //ditto for image 2 [self.view addSubview:view1]; views = [[NSArray alloc] initWithObjects:view1, view2, nil]; pageControl.numberOfPages = [views count]; pageControl.currentPage = 0; } @end` – marciokoko Jan 09 '13 at 22:12
  • Ok ive got it set up with 3 programmatically created UIImageViews each with a UIImage. I understand the concept of calling changePage and it works, but I understand I need to place the first uiimageview on the main view and then everytime the changePage method is called I have to "add 320 px to its position, remove it from view, add 320 pixels to the new view"? – marciokoko Mar 12 '13 at 20:10
  • Yes, but rather than hard-coding 320, it's better to calculate the position based on the superview's bounds. – Cory Kilger Mar 21 '13 at 04:42