Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 视图控制器视图仅添加一次_Ios_Uiviewcontroller_Childviewcontroller - Fatal编程技术网

Ios 视图控制器视图仅添加一次

Ios 视图控制器视图仅添加一次,ios,uiviewcontroller,childviewcontroller,Ios,Uiviewcontroller,Childviewcontroller,我有一个主视图控制器,它包含一个名为containerScrollView的UIScrollView。此scrollview在每页上都有一个屏幕大小的scrollview,其中包含两个视图控制器:MessagesViewController和InfoViewController。这是一个模式 containerScrollView中的personScrollView工作正常,但问题在于将两个视图控制器的视图添加到personScrollView中 @property (nonatomic, r

我有一个主视图控制器,它包含一个名为containerScrollView的
UIScrollView
。此scrollview在每页上都有一个屏幕大小的scrollview,其中包含两个视图控制器:
MessagesViewController
InfoViewController
。这是一个模式

containerScrollView中的personScrollView工作正常,但问题在于将两个视图控制器的视图添加到personScrollView中

@property (nonatomic, retain) MessagesViewController *matchesVC;
@property (nonatomic, retain) InfoViewController *standingsVC;


for (int i = 0; i < 3; i++) {
    UIScrollView *personScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(i*320, 0, 320, self.containerScrollView.frame.size.height)];
    NSArray *colors = @[[UIColor blueColor], [UIColor orangeColor], [UIColor greenColor]];
    [personScrollView setBackgroundColor:[y objectAtIndex:i]];
    [personScrollView setPagingEnabled:YES];
    [personScrollView setContentSize:CGSizeMake(self.view.frame.size.width * 2, personScrollView)];
    [self.containerScrollView addSubview:personScrollView];

    /* Populate the scrollview */
    // Messages
    if (self.messagesVC == nil)
    {
        self.messagesVC = [[MessagesViewController alloc] init];
        [self.messagesVC setFrame:CGRectMake(0, 0, 320, self.containerScrollView.frame.size.height)];
    }
    [personScrollView addSubview:self.messagesVC.view];

    // Info
    if (self.infoVC == nil)
    {
        self.infoVC = [[InfoViewController alloc] init];
        [self.infoVC setFrame:CGRectMake(320, 0, 320, self.containerScrollView.frame.size.height)];
    }
    [personScrollView addSubview:self.infoVC.view];
}

[self.containerScrollView setContentSize:CGSizeMake(320*3, self.containerScrollView.frame.size.height)];
@property(非原子,保留)MessagesViewController*matchesVC;
@属性(非原子,保留)InfoViewController*standingsVC;
对于(int i=0;i<3;i++){
UIScrollView*personScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(i*320,0320,self.containerScrollView.frame.size.height)];
NSArray*colors=@[[UIColor blueColor]、[UIColor orangeColor]、[UIColor GREENCLOR];
[personScrollView setBackgroundColor:[y对象索引:i]];
[personScrollView设置分页启用:是];
[personScrollView设置内容大小:CGSizeMake(self.view.frame.size.width*2,personScrollView)];
[self.containerScrollView添加子视图:personScrollView];
/*填充滚动视图*/
//信息
if(self.messagesVC==nil)
{
self.messagesVC=[[MessagesViewController alloc]init];
[self.messagesVC setFrame:CGRectMake(0,0320,self.containerScrollView.frame.size.height)];
}
[personScrollView添加子视图:self.messagesVC.view];
//信息
if(self.infoVC==nil)
{
self.infoVC=[[InfoViewController alloc]init];
[self.infoVC setFrame:CGRectMake(320,0320,self.containerScrollView.frame.size.height)];
}
[personScrollView添加子视图:self.infoVC.view];
}
[self.containerScrollView setContentSize:CGSizeMake(320*3,self.containerScrollView.frame.size.height)];
问题是这两个视图控制器(消息和信息)只添加了一次,并且添加到containerScrollView的最后一个personScrollView

如何将视图控制器添加到所有PersonScrollView?财产申报有什么问题吗


我读过一些关于滥用视图控制器的文章,但这是唯一的解决方案。这两个视图控制器中确实有很多代码,我无法将其添加到我的rootviewcontroller中

问题在于您对视图控制器和视图之间差异的理解。你需要仔细阅读。

苹果文档上说:

视图只能有一个superview。如果视图已经有一个superview,而该视图不是接收器,则此方法会在使接收器成为其新superview之前删除以前的superview


创建控制器一次,但要将其视图添加三次到三个不同的父视图。你不能这样做。

我最终创建了视图控制器的多个实例,并将它们存储在一个数组中。这不是一个很好的解决方案,但却是我能找到的最好的解决方案

@property (strong, nonatomic) MessagesViewController *messagesVC1;
@property (strong, nonatomic) MessagesViewController *messagesVC2;
@property (strong, nonatomic) MessagesViewController *messagesVC3;
@property (strong, nonatomic) MessagesViewController *messagesVC4;
@property (strong, nonatomic) MessagesViewController *messagesVC5;
@property (strong, nonatomic) MessagesViewController *messagesVC6;

self.messagesVC1 = [[MessagesViewController alloc] initWithData:data];
self.messagesVC2 = [[MessagesViewController alloc] initWithData:data];
self.messagesVC3 = [[MessagesViewController alloc] initWithData:data];
self.messagesVC4 = [[MessagesViewController alloc] initWithData:data];
self.messagesVC5 = [[MessagesViewController alloc] initWithData:data];
self.messagesVC6 = [[MessagesViewController alloc] initWithData:data];
self.messagesVCArray = @[self.messagesVC1, self.messagesVC2, self.messagesVC3, self.messagesVC4, self.messagesVC5, self.messagesVC6];

MessagesViewController *messagesVC = [self.messagesVCArray objectAtIndex:i];
[messagesVC setFrame:CGRectMake(0, 0, 320, leagueScrollView.frame.size.height)];
[leagueScrollView addSubview:messagesVC.view];

不是真的。我只是想从另一个视图控制器控制scrollview上的视图,以保持代码的条理化,我正在尝试这样做。