首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PageControl和ScrollView组合

PageControl和ScrollView组合
EN

Stack Overflow用户
提问于 2013-07-09 11:07:39
回答 1查看 165关注 0票数 0

我是一个新的iOS程序员。当我尝试使用分页来编写ScrollView代码时,有一个问题需要修复。我真的不知道我的代码中有什么问题。我已经设置了pageEnable,并将页码设置为4当前为0。在xib.file中,我创建了4个视图,让scrollview连接“代理”到“文件的所有者”。我的引用链接http://www.iosdevnotes.com/2011/03/uiscrollview-paging/当我运行应用程序时,视图不能分页。谢谢你的帮助。下面是我的代码:

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

导入

@接口ThirdViewController : UIViewController {

代码语言:javascript
复制
IBOutlet UIScrollView *scrollView; 
IBOutlet UIPageControl *pageControl;
IBOutlet UIView *contentOne;
IBOutlet UIView *contentTwo;
IBOutlet UIView *contentThree;
IBOutlet UIView *contentFour;

}

代码语言:javascript
复制
- (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        
EN

回答 1

Stack Overflow用户

发布于 2013-07-09 11:18:54

您是否在它崩溃的行旁放置了断点?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17539240

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档