programmatically adding textview - ios

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

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.

Related

iOS UIView adding and removing subviews throughout the code

I have a blue line in my app that displays an artificial horizon.
I want to be able to display and remove it at different parts of the code (.m).
Can I declare it (lineViewHorizon) universally.
At present I can make it appear/disappear only within the same section/method of code.
I presume this can be done?
UIView *lineViewHorizon = [[UIView alloc] initWithFrame:CGRectMake(0, pageTopMargin+inthorizon, self.view.bounds.size.width, 2)];
lineViewHorizon.backgroundColor = [UIColor blueColor];
[self.view addSubview:lineViewHorizon];
[lineViewHorizon removeFromSuperview];
I want to be able to display and remove it at different parts of the code
In order to remove a view from another view, you'll need a pointer to the view that you want to remove. You can get that either by finding the view in the view hierarchy, perhaps using -viewWithTag:, or you can keep a pointer to the view in an instance variable (or property). Either way, the key thing is that you need a pointer to the view so that you can send it a -removeFromSuperview message.

How can I create views with dynamic content in iOS?

I' m encountered with a problem because of my lack of knowledge of iOS platform I think.I have a view controller User Profile. The main view has next structure: ScrollView which contains one subview and this UIView has 7 UIViews with UILabels, UItextViews, UIImageViews, etc. each of them displays specific info about user. The problem - data for this views is fetched from the server and it can be different. E.g I have a subview Education, it contains next info: name of the institute, degree, years of learning, etc. But one user can have several educations. So the size of this view can be dynamic. But i can't simply change the view size with view.frame = CGRectMake because under it I have a view job experience and etc. Or for example professional skills view: it can be one skill and it can be one hundred skills for one user -so I need to change the size of this view but under it I have another view so i need to move it and so on and so forth. So the question what is the correct way of dealing with this situation? I understood that I can't just change view frames with view.frame= CGRectMake() - because it is too much work and it is a stupid approach I think. I know there must be some more straightforward way for this very common problem, may be autolayout or something else? Anyway I hope for some help and advice of how can I make views with dynamic content.
I really don't know why you said "I can't just change view frames". Of course you can!
The approach I always take to this scenario (where your view's height is variable) is to declare a yOffset property and based on this I place my content at the right y position.
#property (nonatomic, assign) int yOffset;
Then inside the init method I initialize this property to either 0 or some predefined initial margin.
_yOffset = TOP_MARGIN;
Then consider the following scenario: a user profile with n number of skills. This is how you would use the yOffset property.
for (int i=0; i<n; ++i)
{
// place the skillView at the right 'y' position
SkillView *skillView = [[SkillView alloc] initWithFrame:CGRectMake(0,self.yOffset,300,50)];
// configure the skillView here
[self.view addSubview:skillView];
// Don't forget it has to be "+=" because you have to keep track of the height!!!
self.yOffset += skillView.frame.size.height + MARGIN_BETWEEN_SKILLS;
}
And then imagine the user profile has c number of Education entries; same thing:
for (int i=0; i<c; ++i)
{
// place the educationView at the right 'y' position
EducationView *educationView = [[EducationView alloc] initWithFrame:CGRectMake(0,self.yOffset,300,50)];
// configure the educationView here
[self.view educationView];
// Don't forget it has to be "+=" because you have to keep track of the height!!!
self.yOffset += educationView.frame.size.height + MARGIN_BETWEEN_EDUCATIONS;
}
Finally, when all your subviews have been added you have to change the frame of the containing view. (Because when you created it you couldn't know upfront how tall it was going to be)
CGRect viewFrame = self.view.frame;
viewFrame.size.height = self.yOffset + BOTTOM_MARGIN;
self.view.frame = viewFrame;
And that's it.
Hope this helps!
As you've stated, manually updating the frames is a lot of work and is the old way of doing things. The new better way as you've also said is to use auto layout. Describing how to use auto layout to position your views would require more specific information on how you want it to look but there are some awesome tutorials out there on the web! Here's one of many:
Ray Wenderlich autolayout in iOS 7 part 1

Splitviewcontroller logic is not right, duplicate content

I am trying to port my iphone app to Ipad, the logic does not work with splitviewcontroller.
In viewDidLoad i call a function that updates the view and with setSearchResult (overriding the setters)
the problem is everytime when i select a row in the masterviewcontroller the contents of the detailviewcontroller get added to it instead of removing the old contents..
i think this gives me the problem:
CGRect framephoto = CGRectMake(0, 85, 320, 186);
carousel = [[iCarousel alloc] initWithFrame:framephoto];
[self.scrollview addSubview:carousel];
and this table (custom mg)
// the tables grid
CGSize tablesGridSize = IPAD_TABLES_GRID;
tablesGrid = [MGBox boxWithSize:tablesGridSize];
tablesGrid.contentLayoutMode = MGLayoutGridStyle;
[self.scrollview.boxes addObject:tablesGrid];
I think because everytime when i select a row and it gets to these codes it adds another one.
I am not a pro so maybe this question is stupid, but every tip is welcome..
Whats the best way to handle this ?
Unfortunately, the best way to handle this is to use the UINavigationController you should already have in use in your application. A easy solution for your problem is to just remove all subviews before you are adding new ones (maybe you have to safe the references to the iCarousel and the tablesGrid for this). A even better way is to just update the data of the views you already have allocated.

How can i auto generate text fields

I'm new to iOS and I'm busy with my first app. I have a question: In the code below is there any way to have my app auto generate a text field when a button is pressed without me having to set it up first?
For example in the code below would it be possible to get input from a text field and use it in place of "field1" to auto generate another textfield.
UITextField *field1 = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 100, 30)];
field1.placeholder = #"Textbox 1";
field1.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:field1];
x = 10;
w = 100;
h = 30;
y = y + 60;
UITextField *field1 = [[UITextField alloc] initWithFrame:CGRectMake(x, y, w, h)];
field1.placeholder = #"Textbox 1";
field1.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:field1];
OK i see it was my code. In CGRectMake i replaced the values with variables which i incremented now with every button press and its more text fields, before it was adding on on top of the other thats why it seemed like it wasnt adding new text fields.
Now 2 questions on the above,
can i access the textfields individualy using this method.
Is this considered best practice or does it just use a lot of memory.
General Answer
Going by your question and comments, it looks like you want to keep adding an arbitrary number of fields and manage them as you go (keep them around so you can access and manipulate them). You'll still need to write code to create a field and stash it somewhere.
You can create a -createAndStoreFieldWithName: method that's called by your action. Such a method would use something similar to the code you wrote above but would store it somewhere. I'd recommend an 'NSMutableDictionary' that lives permanently in your controller, since dictionaries provide named access to their content.
This way, if you want to create a field called "Foo", your -createAndStoreFieldWithName: method would check to see if an entry named Foo already exists in the fieldsDictionary dictionary and, if not, would create the field (and add it to its superview, positioned wherever you wish) in code and call -[fieldsDictionary setObject:newlyCreatedField forKey:fieldName] to store it. That way, you can always get the field by name by asking -[fieldsDictionary objectForKey:desiredFieldName].
If you later want to remove them, use the same "get the field by name" approach to access the field, remove it from its superview, and remove it from the dictionary so it can be disposed of properly.
Of course if multiple fields with the same name can exist (ie, more than one Foo field), you'll need to add a layer of abstraction. In this case, you can use a unique identifier (like a UUID) the user never sees. The thing to figure out (which is hard to specify without more detail from you) is how you'll match the identifier to the proper field (given the possibility of multiple Foo fields, for example).
Also, you could just add the fields to the superview and loop through its -subviews array to locate fields by their label but this is anti-pattern to proper MVC design. Your controller object (the one creating the labels and adding them to other views) should be keeping track of these fields as I mentioned above, not the superview. This lets your controller be the intelligent mediator, since it knows what the fields are for and will use them (or possibly hand them off to some other controller) as it needs.
Situation-Specific Answer
BUT - consider whether individual UITextFields are the right way to go. Perhaps a UITableView with added rows is the better choice here? In this case your controller just decides there's one more row and kicks the table view to update. When asked about the cell, just give it your desired label from some labels container (maybe just a straight NSMutableArray of labels maintained by your controller - it matches array index to row index). MUCH simpler.
More detail in your question will get you more specific answers. Again, though, edit your original question - don't keep appending comments.
I assume you have a buttonPressed: method? Your question is not very clear, so if you'd like to clarify, I'm sure we can provide you with a better answer. Assuming you have a textField named inputField as a property of your view controller, you could do something like this:
- (void)buttonPressed:(id)sender {
NSString *currentText = self.inputField.text;
UITextField *newTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 100, 30)];
newTextField.placeholder = currentText;
newTextField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:newTextField];
}
This would create a new UITextField upon button press with its placeholder text set to whatever was in your other UITextField. This is assuming you just want to do this once and that you only have a single new UITextField. If you can do this multiple times, but still just want a single UITextField updated with new text, newTextField should be a property, and you check if it is not nil and alloc init if it is. If it's not nil (it already exists), you need only update the text/placeholder property with the new text.
If every time the user enters text, you need a new UITextField in a new location, the solution would be a little different. Again, all of these answers (mine and others) are making assumptions about what you are trying to achieve.
UPDATE: Allow me to update this answer appropriately, as was previously pointed out. The long-and-short of it would be to use #JoshuaNozzi's approach, as it will achieve what you're trying to do. Just updating this for completeness.

iOS automation : how to tap button that has no id

I have the following items in logElementTree output:
UIAButton: rect:{{20, 427}, {41, 41}}
UIAButton: rect:{{140, 427}, {41, 41}}
These buttons have no identifier, no name, and are not drawn in XIB. On my automation test script I only use index (something like target.frontMostApp().mainWindow().buttons()[7].tap())
But then, this line will not always work because index is changing. I just want to ask, if there's a way to tap this button other than using index? Please note that the button has no name, so I cannot use buttons()["name'"].tap()
Technically this would be the best way to go about doing what you would like, so I'll leave it here for other developers who see this question. In your case since you have limited technical experience, I would recommend asking your developer to assign ids or names to buttons. Providing good names for buttons and other UI elements means that your app is also accessible for those users with impaired eyesight, since voiceover will read them the names you've given to your buttons. The following block of code will programmatically assign a label and a accessibility hint to a dynamically created object that conforms to UIAccessibility.
From Apple's documentation (in this case they're doing it on a view, but you could do it on any object like a button):
- (id)init
{
_view = [[[MyCustomView alloc] initWithFrame:CGRectZero] autorelease];
[_view setIsAccessibilityElement:YES];
[_view setAccessibilityTraits:UIAccessibilityTraitButton];
[_view setAccessibilityLabel:NSLocalizedString(#"view.label", nil)];
[_view setAccessibilityHint:NSLocalizedString(#"view.hint", nil)];
}
http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/iPhoneAccessibility/Making_Application_Accessible/Making_Application_Accessible.html#//apple_ref/doc/uid/TP40008785-CH102-SW5
Javascript "hack" (not very clean, but it works...):
var window = UIATarget.localTarget();
window.tap({x:yourXCoordinateHere , y:yourYCoordinateHere});

Resources