带有 ViewControllers 的 UIScrollView

Posted

技术标签:

【中文标题】带有 ViewControllers 的 UIScrollView【英文标题】:UIScrollView with ViewControllers 【发布时间】:2016-06-09 07:11:47 【问题描述】:

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

两个 ViewController(完美运行):

- (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;

三个 ViewController(不起作用):

- (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”

【问题讨论】:

检查 IBOutlet 连接***.com/questions/34581479/… 你会用xib吗? 是的,我正在使用 xib 文件 @SurajSukale @kb920 感谢您的评论,但我是编程新手,我真的不明白那个问题的答案......你能直接帮我吗?我真的很感激! 尝试从视图而不是文件的所有者连接所有插座。 【参考方案1】:

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

【讨论】:

我是编程新手,很抱歉,我不明白你想让我在这里做什么。感谢您的帮助!你能再具体一点吗?或者更多地指导我需要做些什么来解决这个问题?非常感谢! 您应该寻找从 AViewController 对您的 ViewController 类的调用。【参考方案2】:

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

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;

【讨论】:

以上是关于带有 ViewControllers 的 UIScrollView的主要内容,如果未能解决你的问题,请参考以下文章

UIScrollView 常见于不同的 viewControllers

如何通过 ViewControllers 管理和释放内存

将相同的 UITableView 添加到不同的 ViewControllers

iOS 检查 ViewController 实例是不是打开并切换 ViewControllers

手动操作 navigationController 的 viewControllers 数组时 iOS 手机崩溃

仅在不切换 viewControllers 的情况下 Segue 动画