UIScrollView委托属性无法正常工作

时间:2014-07-26 00:05:07

标签: ios objective-c uiscrollview delegates scroll-paging

我的问题是我的scrollView的属性(是正确的单词?)不起作用。我试图让scrollView到页面,但似乎它忽略了我的代码行

[scrollView setPagingEnabled:YES];

scrollView上下左右滚动(设置了contentSize),但它没有捕捉到页面,并且测试其他没有工作的委托属性似乎问题是结果。

似乎我在声明委托时做错了什么,这应该是自我的。这是我的.h标题,使DayViewController成为UIScrollView委托

@interface DayViewController : UIViewController <UIScrollViewDelegate> {
UIScrollView *scrollView;
//other code......
}

以下是我的.m文件的相关部分,其中委托设置为self,我尝试调整UIScrollView的属性。

- (void)viewDidLoad
{
[super viewDidLoad];
tester = [Global tester];
cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
viewsInMemory = [[NSMutableArray alloc] init];

for (int i = 0; i < 3; i++) {
    [viewsInMemory insertObject:[NSNull null] atIndex:i];
}

scrollView = [[UIScrollView alloc] init];
scrollView.delegate = self;
[scrollView setScrollEnabled:YES];
[scrollView setPagingEnabled:YES];
scrollView = [[UIScrollView alloc] initWithFrame:(CGRectMake(0, 0, 320, self.view.frame.size.height))];
scrollView.backgroundColor = [UIColor lightGrayColor];

[self loadInitialDays];


[scrollView addSubview:(currentDayView)];
[self.view addSubview:(scrollView)];

}

我理解我的代码的其他部分可能看起来效率低下或其他什么,但这不是我要求帮助的。我唯一需要的就是让你们中的一个好人找出代表为什么不工作。非常感谢!

1 个答案:

答案 0 :(得分:2)

问题在于这一行:

scrollView = [[UIScrollView alloc] initWithFrame:(CGRectMake(0, 0, 320, self.view.frame.size.height))];

您重新初始化scrollView,因此忽略了上述所有属性,并且实际设置的唯一属性是backgroundColor属性。

要解决此更改此行:

scrollView = [[UIScrollView alloc] init];

scrollView = [[UIScrollView alloc] initWithFrame:(CGRectMake(0, 0, 320, self.view.frame.size.height))];

所以你的最终代码如下:

scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height)];
scrollView.delegate = self;
[scrollView setScrollEnabled:YES];
[scrollView setPagingEnabled:YES];
scrollView.backgroundColor = [UIColor lightGrayColor];
相关问题