【问题标题】:Adding UIView to UIViewController dynamically动态添加 UIView 到 UIViewController
【发布时间】:2015-06-14 16:56:52
【问题描述】:

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

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

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

1.) 我不想为 UIView 使用 XIBfile,但只想使用 StoryBoard。我如何在StoryBoard 中设计UIView

2.) 随着数组中项目数量的增加,如何将UIView 动态添加到UIViewController

【问题讨论】:

  • 解释更多关于你想要添加的视图的事情。喜欢它的高度宽度,你想添加这个视图 sideByside 或 oneByone verticle??
  • 这是一个沼泽标准集合视图。不要尝试重新创建它,因为 Apple 已经完成了跟踪布局并根据需要包装视图的艰苦工作。下面有一个答案,详细说明了如何解决这个问题,我建议你遵循它并为自己省去很多悲伤。

标签: ios objective-c iphone uiview uiviewcontroller


【解决方案1】:

遍历您的数组(我假设您的数组包含 UIViews,如果没有,您可以相应地更新),例如:

for(UIView *subView in arrayOfItems){
       subView.position = specify the position 
       [self.view addSubview:subView];
}

【讨论】:

    【解决方案2】:

    1) 你需要创建一个类,其中propertiesIBOutlets,像这样:

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

    您可以在UIStoryboard 中设计您的UIView,并将它们从类连接到ui 元素。在之前的xCode 版本中,如果你想从UIStoryboard 拖动到班级,总是会出现问题,必须以不同的方式进行。

    2) 要将UIView 添加到您的UIViewController,您只需将其添加为subView 在您的UIViewController 中,如下所示:

    [self.view addSubView:anyView]
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

      • 如何将其附加到下一行。例如,如果只有 2 个 UIVIews 适合一行,我将如何将其附加到下一行?
      【解决方案4】:

      在情节提要中创建一个 UICollectionView ,并添加一个 UICollectionViewCell 原型,其中包含一个 UIButton ,并在 UICollectionViewDataSource 方法中 numberOfItemsInSection 返回您的数组计数:

      -(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 类型的属性(不要忘记将情节提要中的按钮类更改为新的 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。

      【讨论】:

        猜你喜欢
        • 2015-09-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-02
        • 2011-06-15
        • 1970-01-01
        • 2011-06-14
        • 1970-01-01
        相关资源
        最近更新 更多