2013-03-01 83 views
0

我是小白到iPhone開發(第3天在Xcode),我想實現的PageControl和滾動視圖,使用戶可以在各種頁面之間輕掃。我正在使用this教程,我無法弄清楚如何從nib文件加載/切換視圖,而不是僅僅改變視圖的背景顏色。任何幫助是極大的讚賞。如何使用PageControl和UIScrollView切換視圖?

我的代碼

PageControlExampleViewController.m改造更名NewsClass2

// Creates the color list the first time this method is invoked. Returns one color object from the list. 
+ (UIColor *)pageControlColorWithIndex:(NSUInteger)index { 
if (__pageControlColorList == nil) { 
    __pageControlColorList = [[NSArray alloc] initWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor magentaColor], 
           [UIColor blueColor], [UIColor orangeColor], [UIColor brownColor], [UIColor grayColor], nil]; 
} 
// Mod the index by the list length to ensure access remains in bounds. 
return [__pageControlColorList objectAtIndex:index % [__pageControlColorList count]]; 
} 

//Changing views instead of colors, not working 
+ (UIView *)pageControlViewWithIndex:(NSUInteger)index { 
if (__pageControlViewrList == nil) { 
    __pageControlViewrList = [[NSArray alloc] initWithObjects:[[UIView alloc] initWithNibName:@"PageView" bundle:nil], [[UIView alloc] initWithNibName:@"PageView" bundle:nil], [[UIView alloc] initWithNibName:@"PageView" bundle:nil], 
           [[UIView alloc] initWithNibName:@"PageView" bundle:nil], [[UIView alloc] initWithNibName:@"PageView" bundle:nil], [[UIView alloc] initWithNibName:@"PageView" bundle:nil], [[UIView alloc] initWithNibName:@"PageView" bundle:nil], nil]; 
} 
// Mod the index by the list length to ensure access remains in bounds. 
return [__pageControlViewList objectAtIndex:index % [__pageControlViewList count]]; 
} 

// 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 = [NewsClass2 pageControlColorWithIndex:pageNumber]; 
//Setting View Not Working 
self.view = [NewsClass2 pageControlViewWithIndex:pageNumber]; 
} 
+0

你只是試圖滾動到一個特定的「頁面」在滾動視圖?無論如何,這個教程很奇怪,因爲它在viewDidLoad中設置了文本和背景顏色。 scrollview中的每個視圖都沒有設置好嗎?所以只需顯示scrollview的那部分。沒有真正詳細解讀它。抱歉。 – Fraggle 2013-03-02 13:36:44

回答

0

歡迎目的C.

所有你需要設置滾動視圖的委託像

//in .h file write 

@interface ViewController : UIViewController<UIScrollViewDelegate> 



// in viewDidLoad 

self.scrollView.delegate = self; 
第一

然後寫在UIScrollView的委託方法寫...

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

然後作出valueChange的IBAction爲對像的PageControl .....

- (IBAction)changePage:(id)sender 
{ 
    int page = self.pageControlHelp.currentPage; 
    CGRect frame = self.scrollViewHelp.frame; 
    frame.origin.x = frame.size.width * page; 
    frame.origin.y = 0; 
    [self.scrollViewHelp scrollRectToVisible:frame animated:YES]; 
} 

而且你已經做了.......

如果你有這方面的任何混亂請隨時問......