2011-10-29 10 views
0

私はビューを変更せずに、配列内のエントリを切り替えるためにページコントロールを使用することに興味があります。これどうやってするの?チュートリアルはありますか? 。 。ページコントロールを介して配列

ただし、ページが切り替わるたびに、背景色と一部の画像が変化します。ページが切り替わるとコードを実行する方法はありますか?

おかげ

答えて

3

UIPageControlのみそれはページング動作自体を管理していない、ページングを表します。 UIPageControlをクリックすると、配列を反復処理するためのコードを記述する必要があります。 currentPageプロパティは、配列内で操作しているインデックスに設定する必要があります。totalPagesプロパティは、配列のcountプロパティに設定する必要があります。最後に、あなたは-addTarget:action:forControlEvents:

[pageControl addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged]; 

documentation statesを使用してUIControlEventValueChangedイベントのためのセットアップにターゲット/アクションのペアが必要になります:

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. The delegate can then evaluate the currentPage property to determine the page to display. The page control advances only one page in either direction.

ですから、ページが変更されたときに決定するためにそのイベントを使用することができます。

例:

@property (weak, nonatomic) IBOutlet UIPageControl *pageControl; 
@property (strong, nonatomic) NSArray *items; 

[...]

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
    self.items = [NSArray arrayWithObjects:@"One",@"Two",@"Three",@"Four",@"Five",@"Six",@"Seven",@"Eight",@"Nine",@"Ten", nil]; 
    [self.pageControl addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged]; 

    // Left Swipe to go to previous page 
    UISwipeGestureRecognizer *leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(previousPage:)]; 
    leftSwipeGestureRecognizer.direction = numberOfTouchesRequired = 1; 
    leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft; 
    [self.view addGestureRecognizer:leftSwipeGestureRecognizer]; 

    // Right Swipe to go to next page 
    UISwipeGestureRecognizer *rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(nextPage:)]; 
    rightSwipeGestureRecognizer.direction = numberOfTouchesRequired = 1; 
    rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight; 
    [self.view addGestureRecognizer:rightSwipeGestureRecognizer]; 

} 

- (void)previousPage:(UISwipeGestureRecognizer *)swipeGestureRecognizer 
{ 
    self.pageControl.currentPage = self.pageControl.currentPage-1; 
} 

- (void)nextPage:(UISwipeGestureRecognizer *)swipeGestureRecognizer 
{ 
    self.pageControl.currentPage = self.pageControl.currentPage+1; 
} 

- (void)pageChanged:(id)sender 
{ 
    NSString *selectedItem = [self.items objectAtIndex:self.pageControl.currentPage]; 
    NSLog(@"\nSelected Item: %@\nCurrent Page: %d\nTotal Number of Pages: %d\n", selectedItem, self.pageControl.currentPage+1, self.pageControl.numberOfPages); 
} 

ページコントロールをクリックすると、それはself.itemsプロパティによって表される文字列の配列を前後に移動します。 -pageChanged:は、ビューのコントロールも更新する場所です。

あなたは別の領域にUIPageControlで現在のページを更新したい場合は、単に実行します。

self.pageControl.currentPage = 5; // set to whatever page you need represented 
+0

を使用すると、コードの例をいくつかのより多くを説明していただけます、それは私がページコントロールを使用しています初めてです。 –

+0

返信をコード例で更新しました。 – Andrew

+0

OK、NSLogは表示されず、実際に行を変更するコードですか?もしそうなら、ラベルを表示するコードはリロードする必要がありますか? –