Reputation: 16849
I have an Array of 5 items. This array is dynamic therefore the number of items may vary.
I have aUIVIewController
as shown in the following image. In my UIView
, there are few components (Like buttons, etc). Based on the number of items in the above array i want to add the UIVIew
to my UIViewContorller
as shown in the image.
For example: There're 5 items in the Array, then i need to add 5 UIView
's to my UIViewController
.
1.) I don't want to use a XIB
file for the UIView but want to use only StoryBoard
. How can i design the UIView
in StoryBoard
?
2.) How can i add UIView
to the UIViewController
dynamically as the number of items in the array increased ?
Upvotes: 1
Views: 2569
Reputation: 685
create a UICollectionView in the storyboard , and add a UICollectionViewCell prototype with a UIButton in it , and in the UICollectionViewDataSource method numberOfItemsInSection return your array count:
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return yourArray.count;
}
and in order to handle the button touch event ,you'll have to set the UIButton tag to 999 for example in the UICollectionViewCell prototype in storyboard,and in the UICollectionViewDataSource method cellForItemAtIndexPath get a reference to the button using its tag then add the touch event handler to it programmatically :
-(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;
}
finally to find out which button was actually touched (at which indexPath) , i suggest subclassing the UIButton class and add a property of type NSIndexPath (don't forget to change the button class in storyboard to your new UIButton subclass) and in the UICollectionViewDataSource method cellForItemAtIndexPath do the following
-(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;
}
this way in the buttonTouchHandler: method you can check the button property indexPath.
Upvotes: 0
Reputation: 1606
set the frame of the sub views as per your design
for (int i=0;i<YourViewArrayCount; i++) {
[self.view addSubview:[YourViewArrayCount objectAtIndex:i]];
}
Upvotes: 0
Reputation: 6052
1) You need to create a class, where the properties
have IBOutlets
, like this:
@interface MyView ()
@property (weak, nonatomic) IBOutlet UIView *viewContent;
@end
Than you can design your UIView
inside the UIStoryboard
and connect them from the class to the ui element. In previous xCode
versions there was always a problem if you want to drag from UIStoryboard
to the class, have to do it the different way.
2) To add a UIView
to your UIViewController
you just have to add it as subView
inside your UIViewController
like this:
[self.view addSubView:anyView]
Upvotes: 0
Reputation: 279
Loop through your array (I have assumed your array contains UIViews, if not you may update accordingly) like:
for(UIView *subView in arrayOfItems){
subView.position = specify the position
[self.view addSubview:subView];
}
Upvotes: 1