textFieldDidEndEditing - textfield limited value entry - swift

I am attempting to create a calculator that calculates results as UITextField values are changed. There is no submit button I figured textFieldDidEndEditing. Everything works with the exception of the UITextField where the user enters a value. The other fields on the form are UIPickerViews. It limits entry to 1 character in the free form UITextField.
self.mortgageAmount.delegate = self
func textFieldDidEndEditing(_ textField: UITextField) {
let selectedMortgageAmount = Double((mortgageAmount.text)!)
}

If you don't have a submit button and want dynamic calculation while typing into the field, you need to use textField(_:shouldChangeCharactersIn:replacementString:), not textFieldDidEndEditing(_:).
The former triggers each time any contents in the text field are about to change. The latter triggers only when the text field is done editing, either via a submit button or some other action, like scrolling the parent scroll view that is configured to end editing on scroll.
See https://developer.apple.com/documentation/uikit/uitextfielddelegate/1619599-textfield

Related

Edit Name of cell upon tap - Swift

I want to edit the text in a cell by tapping and I was wondering if there is a way to do it other than by adding a text field. is there something in the table view delegate that I missed?
You can create cell (or any other UIResponder subclass) that conforms UITextInput protocol (or maybe UIKeyInput is enough). Here is simple example (old link)
Notice,
you should set cell.canBecomeFirstResponder = true.
To show keyboard use cell.becomeFirstResponder()
Maybe there something else, that you should do. I recommend reading docs.
...Or use UITextField :)
We use an array to display no of text label in cell.
so you can do is, you can use a text field and a button on a view.
In button action add a line of code like stringArray[2] = "new string" to replace the particular element from array and when you enter a text and press a button the element will get replaced and just reload the table view.
And you can also write the method in didSelectRowAtIndexPath, write stringArray[indexPath.row] = "new string" and reload the table view and it is done.

reload tableview without hiding keyboard

I have a custom table view cell that's the first cell in my table view, in which I have a textfield I'm trying to make function like a search feature.
I want the data in the remainder of the table view to update as the user types. I have the search cell's textField set up via .addTarget to trigger EditingChanged so every time the user types something new into the textField it will show the updated results. But it seems that calling self.tableView.reloadData() triggers EditingDidEnd and hides the keyboard, after each letter.
Via addTarget to the Search Cell's UITextField, I have a function searchBarEditingChanged(searchTextField: UITextField) where after self.tableView.reloadData() I call searchTextField.becomeFirstResponder()
How can I get around this?
I was considering using a search controller earlier but I didn't like that you have to pull down to make the search bar visible
edit:
tried using this instead
let sectionsToReload = IndexSet(1...sectionHeaders.count)
self.tableView.reloadSections(sectionsToReload, with: .none)
but it crashes on reloadSections: 2017-05-22 15:37:21.600257 MYAPP[565:89501] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3600.6.22/UITableView.m:1284
Keep the textfield and the result in two different sections, on search reload only result section. This should work
Use the UISearchBar—this is what it is for.
view.endEditing(false) in the UITextFieldDelegate method.

Using "viewAtColumn" on a NSOutlineView makes it to lose the ability to collapse or expand

I have a view based NSOutlineView with some NSTextFields on one of it's columns.
To be able to edit any of these fields I have to click on them twice..first to select the row then to another click to select the text field itself and be able to edit it's contents.
The requirement was to be able to select the text fields using a single click. So in the outlineview's delegate i implemented the "outlineViewSelectionDidChange" like this:
func outlineViewSelectionDidChange(notification: NSNotification) {
let selected:NSInteger = settingsTable.selectedRow
let selectedRow = settingsTable.viewAtColumn(1, row: selected, makeIfNecessary: false)
let field = selectedRow.settingTextField as NSTextField
field.becomeFirstResponder()
}
where settingsTable is an outlet to my NSOutlineView
This works as needed: When I click on any of the text fields inside the OutlineView the current row is selected and the textfield can be edited without the additional click
The problem is that after I clicked on any of the text fields, when I try to expand/collapse the content, I get this error:
Row -1 out of row range [0-4] for rowViewAtRow:createIfNeeded:
Which is caused by the "viewAtColumn" method.
Is there an alternative to the "viewAtColumn" method or another way to get a specific NSTableCellView view using it's row and column?
Using a cell based NSOutlineView is not possible due to layout requirements outside my control.

How i can fetch what is typed between 2 UITextViews on iphone?

i have two UITextView items, how can i fetch what is written using a button on iphone?
Imagine something like a translate app, the user enters a word in UITextView 1 and by pressing the button the UITextView 2 is getting filled with data.
UITextView has a property text. Simply use this.
Set up IBOutlets for textView1 and textView2. Then have the button do something along these lines:
-(IBAction)moveTextOver:(id)sender {
[textView2 setText:textView1.text];
}
To get fancier, you can have a method -(NSString *)transformText:(NSString *)text that translates or does whatever you like. Then use
-(IBAction)moveTextOver:(id)sender {
[textView2 setText:[self transformText:textView1.text]];
}
Create an IBAction method that is linked to a button and in that method read the "text" property of the textView or textField, do your calculations on it and assign the results to the text property of thee second field.

UITextField SecureTextEntry field changes the keypad from numberpad to generic keypad

I have a textField created in IB. I have set the keypad type to Numeric Pad in IB.
When i make secureTextEntry = YES in -(void)textFieldDidBeginEditing:(UITextField *)textField method , my keypad changes from numberpad to generic keypad. I even tried to make the keypad to be numeric programatically but still it doesnot changes.
I have set it like this
-(void)textFieldDidBeginEditing:(UITextField *)textField{
if(textField == self.activationCodeTextField){
self.activationCodeTextField.secureTextEntry = YES;
self.activationCodeTextField.keyboardType = UIKeyboardTypeNumberPad;
}
}
FYI,
I cannot set the textField to be secure in IB because i have an hint text like "4-digits" displayed to the user in the textfield till he starts typing in the text filed. Once he starts typing in the textfield, i want the entries to be a secure text so that it displays only * instead of actual characters he types.
Can you please let me know whats wrong in what I am doing?
I also have two more query
I have a textField where i have some default text (like hint text). I want this hint text to be displayed to the user till the moment he starts typing. I dont want to clear this text when the user clicks on the textfield and then the default text clears away. but, i want this text to be displayed till the moment he actually starts to type something on the keypad, then the default must be cleared and then the actual typed in text to be displayed on the textfield. IS it possible to acheive this ?
Is it possible to set the cursor position of textfield to the first character programatically. This is needed because, i have some default text (hint text) and the cursor is at end of the text. I want the cursor to be at the start of the text. How to make this possible programatically?
Have you tried using -setPlaceholder: on your UITextField? That way you wouldn't need to do put text in the text field, and then later manually convert it to be secure. e.g.
- (void)viewDidLoad {
...
[textField setSecureTextEntry:YES];
[textField setPlaceholder:#"4-digits"];
...
}
That will completely take care of your last two questions. Regarding the first, though, I'm not sure if you can have a numeric keyboard for a secure UITextField or not. It would seem that you can't if setting it programmatically has no effect.