1

Usually I do all of the creation of views in storyboard but now I need to add a view via code and although I understand the very basics of doing one, I need some help on how I go about setting up views but more than that I need to get them placed inside and existing view and also create multiple items. So I will have a parent UIScrollView and inside of that one I need to be able to create up to x additional blocks each block containing two lables and one textview. Something along these lines

I can use the storyboard to create the Parent ScrollView

ParentSCROLLVIEW

child_1_block

lable_1_1

label_1_2

textview_1

child_2_block

lable_2_1

label_2_2

textview_2

child_3_block

lable_3_1

label_3_2

textview_3

child_4_block

lable_4_1

label_4_2

textview_4

. . .

child_x_block

lable_x_1

label_x_2

textview_x

I am trying to create just a single one now but I am not sure of the syntax to place it inside of an existing scrollview currently it is going into the main view working on that right now. The other confusing this I have been thinking about is teh creation of a dynamic amount of the same type of objects are is the object name determined if it is going to be dynamic

UITextView *newTextView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, 300, size.height + 16)];

Currently I use this to create a new textview but I will need to do the same thing for multiple entities

such as *newTextView1, *newTextView2, *newTextView3, *newTextView4, ..., *newTextViewX

Am I able to construct a string with the appended number and then use that string as the name of the object I need to create...never did that before so I am not sure but I have a feeling I would see errors

I am hoping some one could show me some sample code or point me in the right direction or even suggest what the correct terms I can search for...anything would be helpful

Jeff

1
  • found the insert subview information so I can now work on that but the dynamic names is still an issue I do not understand. Commented Jan 6, 2014 at 21:07

1 Answer 1

2

You can't give dynamic names to instances. What you can do, is give them different tags, in case they subclassing from UIView (Like UITextView).

Run this in a loop:

UITextView *myTextView = [[UITextView alloc] initWithFrame....];
[self.view addSubview:myTextView];
myTextView.tag = loopIndexInt; // This is where you put your dynamic number.

Now, In order to retrieve a specific UITextView by it's tag, do:

UITextView *textView = [self.view viewWithTag:5]; // in order to get textView with tag number 5.

Anyway, it really depends on what you're trying to do.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.