iOS Save Text from Dynamic TableViewCell - ios

I am building a part of my app that uses a Dynamic TableView with a UITableViewCell class with 1 label and two UITextFields. One textfield is for the user to type a question and the other is for them to type an answer. The goal is to save the text from the two text fields into CoreData. I am however unsure how to correctly identify one textfield from another since there may be 4 or 5 rows with the same text fields in the table.
I've attached a screen shot of what it looks like.
Should I be using tags on each textfield? If so, how can I set that up?
Thanks for the help!

I'm not sure your exact setup, but it seems easiest to use tags.
When you set the placeholder text for each "He said/I said," also add a line textField.tag = <uniqueTag> where you (arbitrarily) come up with a unique tag for each UITextField that you have. Then, when the user hits save, or when you get a call to a delegate method for all of these text fields, you can differentiate them by checking their tag property.

Related

Inline button and/or textfield within string which wraps the same as text using Swift

This is more of a question of how to go about achieving it rather than a specific solution. I would like to have a label which allowed buttons and textfields to be inline and wrap to the next line. An example is the iOS shortcuts app where you can type in the same block as the text, and the textfield wraps along the same line. It is a textfield as there is a caret.
At a highlevel it might look something like this:
where each view follows the line and wraps to the next when needed.
I originally thought about using an NSAttributedString with links that were styled and the link could either act as a button, or open a textfield for input. I tried this and got something which worked but it did not resemble the iOS shortcuts app where the textfield is within the text. I have also thought about using textkit, but I have not gotten that far with this as I am not sure it is the correct approach.
Thank you for any ideas or solutions.
EDIT
I have thought about another way of achieving this but I don't know whether it would work either. The idea would be to use a collectionView of textfields. Some textfields would share the same LayoutManager so that the text is shared across the textfields. I would have to calculate how many textfields to create so that they flowed down the collectionView
In the image, Label 1 is made up of 1 textfield which has editing disabled. TextField 1 is made up of 3 textfields which have editing enabled and the three textfields would share the same LayoutManager. Label 2 is made up of 2 textfields with a shared layout manager, but editing disabled.
Using this approach would mean calculating how many textfields to create for each "Block" (Label or TextField) and updating this each time content changes. With this approach, I am only thinking of labels and textfields but the button can be added at another time.
I just started on this, but realised that sharing layout managers disables editing so I don't know whether this would be possible anymore.
Thanks
This is non trivial task for sure, and I don't think there's a ready-for-use solution.
Using NSLayoutManager you can calculate frames of each line/character of your text, and then forbid touches depending on these frames, and add background under editable text.
You have to use UITextView, because you gonna need to forbid user to select part of the text, and you can do it using something like willChangeSelectionFromCharacterRange:toCharacterRange: delegate method, and ofc you need shouldChangeTextIn: to forbid removing non editable text too.

Input handling iOS settings page

I am currently working on a settings page for my app. I am using static table views to setup the UI of the view. My problem is my input handling - i cannot find a structured way of changing the input methods from every cell and saving it.
For example, i have a gender input where i use UIPickerView and i then have a age- and weight input where i use UIDatePickers configured as valuepickers. How do i program these different input methods in a way, that they are structured.
My current thoughts are using a hidden UITextfield to show the input methods and altering as the user touches different cells - but i don't know it that would be correct.
Any help would be much appreciated.
I assume you use UILabels to display input values in UI. If you do, you could replace them with UITextFields to which you assign your pickers as inputViews and then when cell is tapped you call tappedTextField.becomeFirstResponder(). After picker is resigned you update tappedTextField's text value.

Adding hidden field to UITableView

I am working on a UITableView inside a UIViewControlller and I would like to add a new cell to a row, but in a way that the only thing showing on the table would be the user defined name, but hidden data (such as a hyperlink) would be retained too. The purpose is to open such hyperlink when pressing the relative button.
I'm using
[dataSource addObject:UserDefineddName];
and this adds the name to the row, but how can I add the data too, without displaying it?
I have labels and a button set up in IB and I was thinking that perhaps the link could be assigned to a hidden label, but it's not clear to me how to do it.
Please advise!
Many thanks
Create a class for your data with two properties, name and url. Then simply fill your data source with instances of this class instead of simply strings. In your cellForRowAtIndexPath do something like this:
titleLabel.text = [dataSource objectAtIndex:indexPath.row].name;

"Compact" view in iOS

I have a view in iOS (iPhone) that have multiple components, organized in sort of a stack way (one in top of the next). Those are user account properties, some could be blank.
So, I have in my view the components layout like this:
UITextField1 (Name)
UITextField2 (Location)
UITextField3 (Age)
UITextView1 (Bio)
UITableView (user entries).
Some of the fields could be blank. Instead of having blank spaces for the blank fields I would like the next field to move upper.
This is like this question of flowlayout: What is the best/easiest way to create 'flow layout' type layout in iOS.
I can only see two ways of dealing with this:
Creating a function that traverse all the UIViews and determines which ones are blank and move the following upper.
Creating a UITableView and use different cell heights for cells whose content is empty.
Ideally there would be a component, but I cannot find it (basically some sort of stack/flow layout).
Anyways, I believe that I am going to implement the option #1 above, but I don't know if there is an "standard" way of accomplishing this (I honestly don't even know the proper term to look for this feature).
Thanks.
I would lean more in the direction of your second choice by using a UITableView but not the way you propose.
This would be my approach using a UITableView:
Create a UITableViewCell (or custom cell) for each one of my
components and assign a tag value to each, we'll use these later. You can do this in viewDidLoad.
Add code in numberOfRowsInSection to check to see which fields have
data values present. Return the total count of the number of fields
with data values
In cellForRowAtIndexPath again, check if data exists for that field
If so, check to see if the cell created in step one has already been created or not (if not, create)
If not, increment a counter of some sort to increase your tag value and find the next field that has a value. Once found, use that "tag/index" number to "return" the proper cell.
In the end, you have a UITableView only displaying the fields with data.

How to delete the contents of a UITextField programmatically?

I am writing an iPhone app that has a number of UITextFields that requires the user to input a bunch of data. I would like to have a simple 'delete' facility that allows the user to discard any data they have put in to the multiple fields.
I've been struggling with this issue for a few days now. I can't seem to find a way of reaching into the individual text fields and changing the text. My app throws errors at me no matter what I try.
You can just assign tags to each textfield in a sequence using tag property in interface builder and then get the textField like this
UITextField *txtf = (UITextField*)[self.view viewWithTag:aTagValue];
txtf.text = #""; or nil;
There are several ways of tackling this.
You can write code to wipe each of them individually. To do this, you create an IBOutlet for each one, and assign a blank string to their text properties. If you only have a couple of fields, that's simplest. If you have more, it's not so good.
You can make them all children of a common container view and loop over its subviews. That way, you only need to hook up a single IBOutlet.
You can recurse over your entire view hierarchy and blank out all the text fields you find.
Another approach, which isn't very well documented by Apple, is to use an IBOutletCollection. That way, you can assign all of your text fields to the one IBOutletCollection and loop over it. This is probably the simplest, most straightforward way of doing it.

Resources