Ios 将UIView动态添加到UIViewController

Ios 将UIView动态添加到UIViewController,ios,objective-c,iphone,uiview,uiviewcontroller,Ios,Objective C,Iphone,Uiview,Uiviewcontroller,我有一个由5项组成的数组。此数组是动态的,因此项目的数量可能会有所不同 我有一个UIVIewController,如下图所示。在myUIView中,组件很少(如按钮等)。根据上面数组中的项目数,我想将UIVIew添加到我的UIViewContorller,如图所示 例如:数组中有5个项目,然后我需要将5个UIView添加到我的UIViewController 1.)我不想在UIView中使用XIB文件,但只想使用情节提要。如何在情节提要中设计ui视图 2.)当数组中的项目数增加时,我如何将UI

我有一个由5项组成的数组。此数组是动态的,因此项目的数量可能会有所不同

我有一个
UIVIewController
,如下图所示。在my
UIView
中,组件很少(如按钮等)。根据上面数组中的项目数,我想将
UIVIew
添加到我的
UIViewContorller
,如图所示

例如:数组中有5个项目,然后我需要将5个
UIView
添加到我的
UIViewController

1.)我不想在UIView中使用
XIB
文件,但只想使用
情节提要
。如何在
情节提要
中设计
ui视图

2.)当数组中的项目数增加时,我如何将
UIView
动态添加到
UIViewController

在数组中循环(我假设您的数组包含UIView,如果没有,您可以相应地更新),如下所示:

1) 您需要创建一个类,其中
属性
具有
IBOutlets
,如下所示:

@interface MyView ()

@property (weak, nonatomic) IBOutlet UIView *viewContent;

@end
[self.view addSubView:anyView]
然后,您可以在
UIStoryboard
内设计
UIView
,并将它们从类连接到ui元素。在以前的
xCode
版本中,如果要从
UIStoryboard
拖动到类中,总是会出现问题,必须以不同的方式执行

2) 要将
UIView
添加到
UIViewController
中,只需将其作为
子视图添加到
UIViewController
中,如下所示:

@interface MyView ()

@property (weak, nonatomic) IBOutlet UIView *viewContent;

@end
[self.view addSubView:anyView]

根据您的设计设置子视图的框架

 for (int i=0;i<YourViewArrayCount; i++) {
            [self.view addSubview:[YourViewArrayCount objectAtIndex:i]];

        }

for(int i=0;i在情节提要中创建UICollectionView,并添加带有UIButton的UICollectionViewCell原型,在UICollectionViewDataSource方法NumberOfItems部分中返回数组计数:

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return yourArray.count;
}
为了处理按钮触摸事件,您必须将UIButton标记设置为999,例如在序列图像板中的UICollectionViewCell原型中,以及在UICollectionViewDataSource方法cellForItemAtIndexPath中,使用按钮的标记获取对按钮的引用,然后以编程方式向其添加触摸事件处理程序:

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"yourCellIdentifier" forIndexPath:indexPath];

    UIButton *button = [cell viewWithTag:999];

    [button addTarget:self action:@selector(buttonTouchHandler:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}
最后,为了找出实际触动了哪个按钮(在哪个indexPath处),我建议对UIButton类进行子类化,并添加一个NSIndexPath类型的属性(不要忘记将storyboard中的button类更改为新的UIButton子类),并在UICollectionViewDataSource方法cellForItemAtIndexPath中执行以下操作

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"yourCellIdentifier" forIndexPath:indexPath];

    MySepcialButton *button = [cell viewWithTag:999];

    button.indexPath = indexPath;

    [button addTarget:self action:@selector(buttonTouchHandler:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

这样,在buttonTouchHandler:方法中,您可以检查按钮属性indexPath。

解释有关要添加的视图的更多信息。例如,它的高度-宽度,以及您要将此视图并排或逐个垂直添加??这是一个bog标准集合视图。不要尝试重新创建此视图,因为Apple已经完成了跟踪的艰苦工作根据需要布局和包装视图。下面有一个答案,详细说明了如何进行此操作,我建议您遵循此答案,并为自己省去很多麻烦。我如何将其附加到下一行。例如,如果只有2个UIView适合一行,我将如何将其附加到下一行?