我想向我的滾動視圖添加頁面控件,並且遵循了大量的網絡教程,其中大部分使用與此相同的代碼tutorial.但是,一旦將代碼放入我的項目,甚至在我對代碼進行更改以嘗試使其工作的情況下,它也不會。我已經設法讓代碼在頁面控件被按下時工作,但它不適用於頁面滾動。我的問題與this類似,但答案沒有幫助。這裏是我的代碼:頁面滾動視圖滾動時控件不會更改
MainViewController.h
@interface MainViewController : UIViewController
{
UIScrollView *svCollegeMain;
UIScrollView *svCollegePage;
UIPageControl *pcCollege;
UIView *viewP1;
}
@property (nonatomic, retain) IBOutlet UIScrollView* svCollegeMain;
@property (nonatomic, retain) IBOutlet UIScrollView* svCollegePage;
@property (nonatomic, retain) IBOutlet UIPageControl * pcCollege;
- (IBAction)changePage;
@end
和MainViewController.m
@implementation MainViewController
@synthesize svCollegeMain, svCollegePage, pcCollege;
- (void)viewDidLoad
{
[super viewDidLoad];
self.svCollegeMain.contentSize = CGSizeMake(960, 332);
self.svCollegePage.contentSize = CGSizeMake(320, 500);
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
CGFloat pageWidth = 320;
int page = floor((svCollegeMain.contentOffset.x - pageWidth/2)/pageWidth) + 1;
pcCollege.currentPage = page;
}
- (IBAction)changePage
{
CGRect frame;
frame.origin.x = self.svCollegeMain.frame.size.width * self.pcCollege.currentPage;
frame.origin.y = 0;
frame.size = self.svCollegeMain.frame.size;
[self.svCollegeMain scrollRectToVisible:frame animated:YES];
}
#pragma mark - View lifecycle
- (void)viewDidUnload
{
[super viewDidUnload];
self.svCollegeMain = nil;
self.svCollegePage = nil;
self.pcCollege = nil;
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
只是櫃面這使得任何區別,我的觀點闡述了觀點,那麼主滾動這個視圖中的視圖和頁面控制,主滾動視圖中的另一個視圖和滾動視圖(彼此相鄰),最後是第二個滾動視圖中的最終視圖(全部在IB中,不需要太多的代碼)以及所有內容在IB中鏈接起來。
Thankyou SOOO很多!每次我檢查代碼時,該行都直接從我身邊飛過。那和下面的一行都是它所花費的:svCollegeMain.delegate = self; (.m)和.h中的。謝謝 –