2012-06-14 55 views
0

我下載了一個關於UIScorllView和UIPageControl的圓頂。爲什麼UIPageControl不顯示? 這裏是代碼,我是Iphone新手。任何幫助將不勝感激!爲什麼我的UIpageControl不顯示?

ScrollView.M我把ResultViewController放到ScrollView中。我想用PageController滾動resultViewController。

- (void)loadScrollViewWithPage:(int)page 
{ 
    if (page < 0) 
     return; 
    if (page >= pageNumber) 
     return; 


    ResultViewController *controller = [viewControllers objectAtIndex:page]; 
    if ((NSNull *)controller == [NSNull null]) 
    { 
     controller = [[ResultViewController alloc] initWithPageNumber:page locations:existLocations]; 
     [viewControllers replaceObjectAtIndex:page withObject:controller]; 
     [controller release]; 
    } 

    if (controller.view.superview == nil) 
    { 
     CGRect frame = scrollView.frame; 
     frame.origin.x = frame.size.width * page; 
     frame.origin.y = 0; 
     controller.view.frame = frame; 
     [scrollView addSubview:controller.view]; 
    } 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    existLocations = [FileManagerUtil readPlistFileForDictionary:@"Locations" fileName:@"CloudCheckLocations.plist"]; 

    pageNumber = [existLocations count]; 
    NSMutableArray *controllers = [[NSMutableArray alloc] init]; 
    for (unsigned i = 0; i < pageNumber; i++) 
    { 
     [controllers addObject:[NSNull null]]; 
    } 
    self.viewControllers = controllers; 
    [controllers release]; 

    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; 


    [self.navigationItem setTitle:@"NetWork condition"]; 
    scrollView.pagingEnabled = YES; 
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * pageNumber, scrollView.frame.size.height); 
    scrollView.showsHorizontalScrollIndicator = NO; 
    scrollView.showsVerticalScrollIndicator = NO; 
    scrollView.scrollsToTop = NO; 
    scrollView.delegate = self; 

    pageContronl.numberOfPages = pageNumber; 
    pageContronl.currentPage = 0; 

    [pageContronl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged]; 

    [pageContronl setBackgroundColor:[UIColor blackColor]]; 
    [self loadScrollViewWithPage:0]; 
    [self loadScrollViewWithPage:1]; 
    [scrollView addSubview:pageContronl]; 

    [self.view addSubview:scrollView]; 

} 



- (void)scrollViewDidScroll:(UIScrollView *)sender 
{ 

    if (pageControlUsed) 
    { 

     return; 
    } 

    CGFloat pageWidth = scrollView.frame.size.width; 
    int page = floor((scrollView.contentOffset.x - pageWidth/2)/pageWidth) + 1; 
    pageContronl.currentPage = page; 

    [self loadScrollViewWithPage:page - 1]; 
    [self loadScrollViewWithPage:page]; 
    [self loadScrollViewWithPage:page + 1]; 

} 

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView 
{ 
    pageControlUsed = NO; 
} 

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 
{ 
    pageControlUsed = NO; 
} 

- (IBAction)changePage:(id)sender 
{ 
    int page = pageContronl.currentPage; 

    [self loadScrollViewWithPage:page - 1]; 
    [self loadScrollViewWithPage:page]; 
    [self loadScrollViewWithPage:page + 1]; 

    CGRect frame = scrollView.frame; 
    frame.origin.x = frame.size.width * page; 
    frame.origin.y = 0; 
    [scrollView scrollRectToVisible:frame animated:YES]; 

    pageControlUsed = YES; 
} 

ScrollView.h

@interface ScrollViewController : UIViewController<UIScrollViewDelegate> 
{ 
    NSMutableArray *viewControllers; 
    NSString *currectNetWork; 
    NSString *flag; 
    NSString *locationName; 

    IBOutlet UIScrollView *scrollView; 
    IBOutlet UIPageControl *pageContronl; 
    BOOL pageControlUsed; 
    int pageNumber; 

    NSMutableDictionary *existLocations; 

} 
@property (nonatomic,retain) NSString *currectNetWork; 
@property (nonatomic,retain) NSString *flag; 
@property (nonatomic,retain) NSString *locationName; 
@property (nonatomic,retain) UIPageControl * pageContronl; 
@property (nonatomic,retain) UIScrollView * scrollView; 
@property (nonatomic,retain) NSMutableArray *viewControllers; 
@property (nonatomic,retain) NSMutableDictionary *existLocations; 
  • (IBAction爲)changePage:(ID)發送者;

ResultViewControl.M 此方法將由ScrollView.M

- (id)initWithPageNumber:(int)page locations :(NSMutableDictionary *) locations 
{ 
    titilArray = [[NSArray alloc] initWithObjects:@"Today", @"Past 7 Day",@"Past 30 Day",nil]; 

    if (self = [super initWithNibName:@"ResultViewController" bundle:nil]) 
    { 
     pageNumber = page; 
     existLocations = locations; 
    } 
    return self; 
} 

回答

1

調用檢查的PageControl和父視圖的背景顏色。如果兩者都具有相同的顏色(默認白色),則頁面控件將不會顯示。

在IOS6中,您有新方法pageIndicatorTintColorcurrentPageIndicatorTintColor

希望這會有所幫助。

0

檢查容器視圖/ tableView的頂部和底部約束(如果存在)。兩者不得附帶「頂部/底部佈局指南」。將它們與容器邊緣連接起來。