Create controls in UIView at runtime? - ios

I have one UITextField and a button in a view.
Once a button is pressed, value in text field need to be searched fieldID column in core data table and get value[comma separated].
Separate the string by comma and assign values to array.
A new UIView needs to be inserted in the main screen and place N number[count of array] of labels and textfields and align.
I am able to do 1, 2 and UIView creation.
Can anybody help me to create N number of controls in sub UIView at runtime?

for(int i=0;i<self.yourArray.count; i++)
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,80*i+60,self.view.frame.size.width-20,60)];
label.text = [yourArray objectAtIndex:i];
[self.view addSubview:label];
}
Please Explain your problem more clearly !

May be your array count is very large so take a scroll view also in your view
UIScrollView* yourScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(yourView.frame.origin.x, yourView.frame.origin.y,yourView.frame.size.width,yourView.frame.size.height)];
for(int i= 0; i< 10;i++)
{
UILabel* lbl = [[UILabel alloc]initWithFrame:CGRectMake(10, y, 50, 21)];
lbl.text = #"";
UITextField* txtField = [[UITextField alloc]initWithFrame:CGRectMake(lbl.frame.origin.x+10 , y, 200, 21)];
txtField.text = #"";
[yourScrollView addSubview:lbl];
[yourScrollView addSubview:txtField];
y = y+30;
}
[yourView addSubview:yourScrollView];

Related

iOS - addSubview without overlap

In a for loop, I'm trying to add Labels to a ScrollView based on a number given by the user. Here is the code in Objective-C:
NSInteger numberOfViews = [self.textfieldNumViews.text intValue];
for (int i = 0; i < numberOfViews ; i++){
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, 300, 12)];
label.text = [NSString stringWithFormat:#"%d",i];
label.backgroundColor = [UIColor lightGrayColor];
[self.scrollviewViewContainer addSubview:label];
}
The problem is that the labels are overlapping each other inside the ScrollView, when I really want them to just display one after the other.
Image I understand that this is due to how addSubview behaves, placing views on top of other views, but is there any way I can prevent that? Or some other function I could use? I feel like this should be an easy task but I can't seem to figure it out. Is there something like a vertical layout property I can add to the ScrollView? Or add margins to the labels?
You need to set the y lower for each subview. Check the third line:
NSInteger numberOfViews = [self.textfieldNumViews.text intValue];
for (int i = 0; i < numberOfViews ; i++){
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(0, (i*20), 300, 12];
label.text = [NSString stringWithFormat:#"%d",i];
label.backgroundColor = [UIColor lightGrayColor];
[self.scrollviewViewContainer addSubview:label];
}

Accessing UILabel created on run time

I have created UILabel (lblCount) and UIButton (btnAdd) on a UIButton (Add Item Button)'s action method. The new UILabel and UIButton is added to scrollview. The UILabel (lblCount) shows count of UIButton (btnAdd) tapped. Here, addBtnClickCount is an integer which counts the number of click.
UILabel * lblCount = [[UILabel alloc] initWithFrame:CGRectMake( 50, 100*addBtnClickCount, 25, 25)];
lblCount.text = [NSString stringWithFormat:#"%d",count];
lblCount.tag = addBtnClickCount;
lblCount.textColor = [UIColor whiteColor];
lblCount.textAlignment = NSTextAlignmentCenter;
[self.scrollView addSubview:lblCount];
addBtnClickCount = addBtnClickCount+1;
There will be multiple label (lblCount) and button (btnAdd) when user taps Add Item Button. I want to access particular label for particular add button and display the count.
You have already set tag on your label. Create a mutable array labelArray and add label to it. To access the particular label do following code on add button's action.
-(void)addItem:(UIButton*)button{
UILabel* lblShowCount = [_labelArray objectAtIndex:[button tag]];
lblShowCount.text = [NSString stringWithFormat:#"%d", [lblShowCount.text integerValue]+1];
}
Here i understand that #Hem Poudyal, know's that with help of viewWithTag.he will get output. but don't know how to achieved that. so i am describing over here.
Step 1: Here i am adding UILabel and UIButton to self.view instead of UIScrollView. i hope you can convert it as UIScrollView. here i applied some relation between UILabel tag and UIButton tag. that you can see in below code.
for (int i=0; i<10; i++) {
UILabel * lblCount = [[UILabel alloc] initWithFrame:CGRectMake( 50, (i*50)+((i+1)*5), 100, 50)];
lblCount.text = [NSString stringWithFormat:#"%d",0];
lblCount.tag = [[NSString stringWithFormat:#"%d%d",i,i] integerValue];
lblCount.backgroundColor = [UIColor yellowColor];
lblCount.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:lblCount];
UIButton* btnTemp = [UIButton buttonWithType:UIButtonTypeCustom];
btnTemp.tag = i;
btnTemp.backgroundColor = [UIColor redColor];
[btnTemp addTarget:self action:#selector(btnTempClick:) forControlEvents:UIControlEventTouchUpInside];
btnTemp.frame = CGRectMake( 150, (i*50)+((i+1)*5), 100, 50);
[btnTemp setTitle:[NSString stringWithFormat:#"Button : %d",i] forState:UIControlStateNormal];
[self.view addSubview:btnTemp];
}
Step 2: In UIButton selector method, Do the following things.
-(IBAction)btnTempClick:(id)sender{
UIButton* btnInner = sender;
NSInteger lblTagbaseOnButtonTag = [[NSString stringWithFormat:#"%ld%ld",btnInner.tag,btnInner.tag] integerValue];
UILabel* lblReceived = (UILabel*)[self.view viewWithTag:lblTagbaseOnButtonTag];
lblReceived.text = [NSString stringWithFormat:#"%ld",[lblReceived.text integerValue]+1];
}
And Output is :
You should have an array that you add the labels and buttons too (this could be one array containing a dictionary or custom class or multiple arrays). Now, when a button is tapped you can find where it is in the array and get the corresponding label to update.
The cheat way is to set the tag of the buttons and labels so you can find one from the other using viewWithTag:.
You need to set unique tag value while creating the labels and buttons and by using viewWithTag: you can access the respective ui container.

Adding multiple text fields with the same code

I want to be able to add between 1 and 20 text fields to my view with the same code.
My idea is to use a for loop and every time add a text field so the first time it loops it adds "textField1" and the second time it adds "textField2" etc.
I'm not sure how I would go about coding this so I'm looking for your help.
Try this code
for 20 textfields you need to take them in UIScrollView .. you can do like this --
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0,0,320,460)];
[scrollView setDelegate:self];
//initialize a textfield array;
NSMutableArray *textFieldArray = [[NSMutableArray alloc]init];
//x, y for first textfield in UIScrollView
float x = 20;
float y = 20;
For(int i=1;i<21;i++)
{
UITextfield *textField = [[UITextfield alloc]initWithFrame:CGRectMake(x,y,200,30)];
textfield.tag = i;
[textField setDelegate:self];
//Add Textfield to array just for futureRefrence
[textFieldArray addObject:textField];
//Add textfield in UIScrollView
[scrollView addSubview:textField];
//Now iterate Y , So they won't over each other
y = y+50;
}
//Now set ScrollView ContentSize
[scrollView setContentSize:CGSizeMake:(320,20*y)];
//Now Finally Add ScrollView in UIView
[self.view addSubView:scrollView];
Also remember declare there delegate in .h file .
and you can access these textfield by the array you put them in ,For Example
If you want a value from textfield three , you can do like this
UITextfield *textfield = (UITextfield *)[textFieldArray objectAtIndex:2];
NSLog(#"text of textField 3 is -- %#",textfield.text);
for (int i = 0; i < 20; i++)
{
UITextField *textField = //setup field
textField.tag = i;
[self.view addSubview:textField];
}
then when you want to reference a field later
UITextField *textField6 = [self.view viewWithTag:6];
UITextField *textField18 = [self.view viewWithTag:18];
etc
NSMutableArray* tfArray = [[NSMutableArray alloc]init];
for (int i = 0; i < 20; i++)
{
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(x, y, width, height)];
[self.view addSubview:textField];
[tfArray addObject:textField];
}
if you want to reference a field:
the 6th textfield is for example: (UITextField*)tfArray[5];

UIScrollView within a panel not scrolling

I'm trying to create a "GridView" style table within a UIScrollview which itself is within a restricted space. I put it within a Scrollview so that the user would be able to scroll down if there are more rows than can be displayed.
My storyboard for the relevant section looks like this:
To test it I tried filling it with UILabels, with this code:
for (int i = 0; i < 15; i++)
{
UILabel *cliLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, (60+(i*30)), 80, 40)];
UILabel *cliColum2Label = [[UILabel alloc] initWithFrame:CGRectMake(110, (60+(i*30)), 150, 40)];
UILabel *cliColum3Label = [[UILabel alloc] initWithFrame:CGRectMake(270, (60+(i*30)), 80, 40)];
UILabel *cliColum4Label = [[UILabel alloc] initWithFrame:CGRectMake(360, (60+(i*30)), 80, 40)];
[cliLabel setText:#"Column 1"];
[cliColum2Label setText:[NSString stringWithFormat:#"Column 2, row %d", i]];
[cliColum3Label setText:#"Column 3"];
[cliColum4Label setText:#"Column 4"];
[self.cliTableScrollView addSubview:cliLabel];
[self.cliTableScrollView addSubview:cliColum2Label];
[self.cliTableScrollView addSubview:cliColum3Label];
[self.cliTableScrollView addSubview:cliColum4Label];
}
This results in the following:
As you can see, there are more rows than can be displayed within the scroll view.
What I'd like to know is how to modify the above so that a scroll-bar appears, giving access to the out-of-bounds rows.
UIScrollView doesn't know how much it has to scroll, you need to tell it by providing contentSize. For example, at the end of your loop:
self.cliTableScrollView.contentSize = CGsizeMake(self.cliTableScrollView.frame.size.width, 60 + i * 30);

How to create dynamic UITextField with different object name?

I want to create dynamic UITextField with different object name.I have shown the code below for creating textfield dynamically. How can i create each textfield with different object name ?
for (int x=0; x < 4 ; x++)
{
CGRect txtFldFrame;
if (x==0)
txtFldFrame=CGRectMake(385, 620, 278, 45);
else
txtFldFrame=CGRectMake(385, txtFldFrame.origin.y+60, 278, 45);
[self createTxtImg:txtImgFrame createTextfield:txtFldFrame];
}
In the
createTextfield:(CGRect )txtfldframe
{
UITextField *txtFld1=[[UITextField alloc]init];
txtFld1.frame=txtfldframe;
[txtFld1 setTag:textChk];
txtFld1.borderStyle = UITextBorderStyleNone;
txtFld1.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
txtFld1.textAlignment=UITextAlignmentLeft;
txtFld1.textColor=[UIColor colorWithRed:17/255.0 green:61/255.0 blue:83/255.0 alpha:1];
txtFld1.font = [UIFont systemFontOfSize:22];
txtFld1.backgroundColor = [UIColor clearColor];
txtFld1.autocorrectionType = UITextAutocorrectionTypeNo;
txtFld1.returnKeyType = UIReturnKeyDone;
txtFld1.clearButtonMode = UITextFieldViewModeWhileEditing;
txtFld1.autocapitalizationType=UITextAutocapitalizationTypeNone;
txtFld1.delegate = self;
[subScrollView addSubview:txtFld1];
}
There is absolutely not a reason to create text fields with a different (unique name).
I'm pretty sure the reason you want the unique name, is so you can access them later in your code and edit their values, but here comes the trick: Use unique tag numbers.
Here is an example:
for (int i=0; i<4; i++) {
UITextField *myTextfield = [[UITextField alloc] initWithFrame:CGRectZero];
myTextField.tag = 200+i;
[self.view addSubview:myTextField];
}
Now we have created 4 text fields, which have tags 200, 201, 202 and 203
Later in code when we want to reference the second text field for example, all we have to do is this:
UITextField *myTextField = (UITextField *)[self.view viewWithTag:201];
now we have the control and we can get or set anything...
As a precaution I would suggest you check you actually got the UI element, by using:
if (!myTextField)
return;
Use following code to create textbox:
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(150, 12, 120, 25)];
[self.view addSubview:textField];
For another textbox just change the object name (textField)
I don't understand everything, because you gave not much information.
If you want to initialize multiple UITextFields:
UITextField *myTextField1 = [[UITextField alloc] init];
UITextField *myTextField2 = [[UITextField alloc] init];
UITextField *myTextField2 = [[UITextField alloc] init];
and so on...
If you want to do this on a random basis or a loop basis, i recommend to loop and put the objects into a dictionary with random keys like so:
NSMutableDictionary *myDict = [NSMutableDictionary dictionary];
for (int i=0; i<20; i++) {
UITextField *myTextfield = [[UITextField alloc] initWithFrame:CGRectZero];
[myDict setObject:myTextfield forKey:[NSString stringWithFormat:#"myTextfield%d", i]];
[myTextfield release];
}
NSLog(#"Dict: %#",myDict);

Resources