PageControl和ScrollView组合

时间:2013-07-09 03:07:39

标签: ios scrollview pagecontrol

我是iOS的新编码器。当我尝试使用分页对ScrollView进行编码时,有一个问题需要修复。我真的不知道我的代码中有什么问题。我已经设置了pageEnable,并设置了4个当前0的页码。在xib.file中,我创建了4个视图,make scrollview将“delegate”连接到“file's owner”。我的参考链接http://www.iosdevnotes.com/2011/03/uiscrollview-paging/ 当我运行应用程序时,无法分页视图。 谢谢您的帮助。 这是我的代码:

//////////我认为viewDidLoad中的for循环存在问题。

import

@interface ThirdViewController:UIViewController {

IBOutlet UIScrollView *scrollView; 
IBOutlet UIPageControl *pageControl;
IBOutlet UIView *contentOne;
IBOutlet UIView *contentTwo;
IBOutlet UIView *contentThree;
IBOutlet UIView *contentFour;

}

- (IBAction)pageValueChange:(id)sender;

@property(nonatomic,strong) IBOutlet UIScrollView *scrollView;
@property(nonatomic,strong) IBOutlet UIView *contentOne;
@property(nonatomic,strong) IBOutlet UIView *contentTwo;
@property(nonatomic,strong) IBOutlet UIView *contentThree;
@property(nonatomic,strong) IBOutlet UIView *contentFour;
@property(nonatomic,strong) IBOutlet UIPageControl *pageControl;
@property(nonatomic,strong) NSArray *viewArray;

@end




#import "ThirdViewController.h"

@interface ThirdViewController ()<UIScrollViewDelegate>

@end

@implementation ThirdViewController

    @synthesize scrollView,contentOne, contentTwo,contentThree,contentFour,pageControl,viewArray;

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
             self.title = NSLocalizedString(@"Profile", @"Third");// Custom initialization
        }
        return self;
    }

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        viewArray = [[NSArray alloc]initWithObjects:@"contentOne",@"contentTwo",@"contentThree", @"contentFour",nil];
        for (int i =0; i < [viewArray count]; i++) {
            CGRect frame;
            frame.origin.x = self.scrollView.frame.size.width * i ;
            frame.origin.y = 0;
            frame.size = self.scrollView.frame.size;
            UIView *subview =[[UIView alloc]initWithFrame:frame];

            [self.scrollView addSubview: subview]; //////how to pass the array object to subview
            [subview removeFromSuperview];

        }
        // Do any additional setup after loading the view from its nib.

        scrollView.contentSize = CGSizeMake(scrollView.frame.size.width *[viewArray count], scrollView.frame.size.height);


    }

    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }


    - (IBAction)dismissKeyboard :(id) sender{
       [sender resignFirstResponder];
    }
    - (IBAction)pageValueChange:(id)sender{

        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;

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


}

@end        

1 个答案:

答案 0 :(得分:0)

你是否在它崩溃的线旁边放了一个断点?