带有ViewControllers的UIScrollView

时间:2016-06-09 07:11:47

标签: ios objective-c uiviewcontroller uiscrollview swipe

我试图在UIScrollView中添加三个UIViewControllers。如果我只有两个,那就完美了。当我添加第三个时,会出现错误并且应用程序崩溃。谁能帮我吗?这是我的代码:

两个ViewControllers(完美运行):

- (void)viewDidLoad{
[super viewDidLoad];



BViewController *bViewController = [[BViewController alloc]init];
CViewController *cViewController = [[CViewController alloc]init];


CGRect frame = cViewController.view.frame;
frame.origin.x = 320;
cViewController.view.frame = frame;



[self addChildViewController:bViewController];
[self addChildViewController:cViewController];


[self.scrollView addSubview:bViewController.view];
[self.scrollView addSubview:cViewController.view];


[bViewController didMoveToParentViewController:self];
[cViewController didMoveToParentViewController:self];


self.scrollView.contentSize = CGSizeMake(320 * 2, self.view.frame.size.height);
self.scrollView.pagingEnabled = YES;}

三个ViewControllers(不能工作):

- (void)viewDidLoad{
[super viewDidLoad];



AViewController *aViewController = [[AViewController alloc]init];
BViewController *bViewController = [[BViewController alloc]init];
CViewController *cViewController = [[CViewController alloc]init];


CGRect frame = cViewController.view.frame;
frame.origin.x = 320;
cViewController.view.frame = frame;

CGRect frametwo = aViewController.view.frame;
frametwo.origin.x = 320 * 2;
aViewController.view.frame = frametwo;



[self addChildViewController:aViewController];
[self addChildViewController:bViewController];
[self addChildViewController:cViewController];


[self.scrollView addSubview:aViewController.view];
[self.scrollView addSubview:bViewController.view];
[self.scrollView addSubview:cViewController.view];

[aViewController didMoveToParentViewController:self];
[bViewController didMoveToParentViewController:self];
[cViewController didMoveToParentViewController:self];



self.scrollView.contentSize = CGSizeMake(320 * 3, self.view.frame.size.height);
self.scrollView.pagingEnabled = YES;}

错误:Here is an image of the error I get 如果您看不到图像,则错误为:" Thread1:EXC_BAD_ACCESS"

2 个答案:

答案 0 :(得分:0)

由于递归,您似乎有堆栈溢出。检查您的子视图控制器(AViewController)是否尝试从ViewController访问viewDidLoad

答案 1 :(得分:0)

我不确定我是否理解你的问题,试试这个

AViewController *aViewController = [AViewController new];
BViewController *bViewController = [BViewController new];
CViewController *cViewController = [CViewController new];

CGRect aFrame = aViewController.view.frame;
CGRect bFrame = bViewController.view.frame;
CGRect cFrame = cViewController.view.frame;

//Setting the frame
aViewController.view.frame = CGRectMake(self.scrollView.frame.origin.x, self.scrollView.frame.origin.y, aFrame.size.width, aFrame.size.height);

bViewController.view.frame = CGRectMake(self.scrollView.frame.origin.x+aFrame.size.width, self.scrollView.frame.origin.y, bFrame.size.width, bFrame.size.height);

cViewController.view.frame = CGRectMake(bFrame.size.width+bFrame.size.width, self.scrollView.frame.origin.y, cFrame.size.width, cFrame.size.height);

aViewController.view.backgroundColor = [UIColor greenColor];
bViewController.view.backgroundColor = [UIColor yellowColor];
cViewController.view.backgroundColor = [UIColor purpleColor];

[self.scrollView addSubview:aViewController.view];
[self.scrollView addSubview:bViewController.view];
[self.scrollView addSubview:cViewController.view];

self.scrollView.contentSize = CGSizeMake(aFrame.size.width+ bFrame.size.width+ cFrame.size.width, self.view.frame.size.height);
self.scrollView.pagingEnabled = YES;