Swift retain text field content in UIPageViewController while swiping - swift

I have a UIPageViewController with multiple pages. Each contains some text fields. Now my problem: When I type in a value into the text field and swipe between the different pages, the text I entered is removed. Does anyone have an idea how to retain the text?

let textFieldData = texField.text
Carry texFieldData with you when you transfer pages
When you get back to that page:
if let text = textFieldData {
textField.text = text
}

Related

NSMutableAttributedString not rendering correctly

I am using a SplitViewController; The master is tableView with a series of RightDetail cells whose Attributed Strings are set via a callback from a value setting popover.
The value passed back and set via a method that sets the detailTextLabel of the cell. It does this within a DispatchQueue.main.async as some of updates to detailTextLabel are made via calls from Network.Framework
The strange thing is when the popover makes the changes the change to the attributed string is not presented correctly. The focus has to change to the next table cell before the correct drawing occurs.
So it is only when focus changes to "Flow Rate" that temperature is rendered correctly, as shown blow.
I have stepped through the following code and checked that the correct value is being written to self.buttonCell?.detailTextLabel?.attributedText
You'll also see that I've added setNeedsLayout() and setNeedsFocusUpdate()
func setDetailValueWithUnit( to index: Int) {
/** sets the detailtext to the value as the specified index and appends units.
it does this within the main dispatchQueue as this method is called by
operationResult which is called by the statemachine.
**/
DispatchQueue.main.async {
let font = self.buttonCell?.detailTextLabel?.font
let finalString = NSMutableAttributedString(string: valueString,attributes: [.font:font!])
if let units = self.units() {
finalString.append(units)
self.buttonCell?.detailTextLabel?.attributedText = finalString
//next 2 lines experimental in an attempt to fix the attributed text not
//being consistently redendered correctly
self.buttonCell?.detailTextLabel?.setNeedsLayout()
self.buttonCell?.detailTextLabel?.setNeedsFocusUpdate()
NSLog("setDetailValueWithUnit: %d", index)
return
} else {
assert(false, "No Units found")
}
}
}
The problem is that what you are doing is completely wrong. You should never be touching a cell's text label font, text, or anything else directly like that. That's not how a table view works. You should set the table's data model and update the table from there, thus causing cellForRowAt: to be called for the visible cells. In that way, only cellForRowAt: configures the cell, and all will be well.

How to remove jump effect when converting single line title into multiline title in iOS 11 LargeTitles

I am trying to show my long text on Navigation Bar in 2 lines. I am using LargeTitles.
What I need is this https://i.stack.imgur.com/Yb85L.png // sorry cant post picture.
Image was taken from this post
I have tried a lot of examples and have achieved in showing the title in 2 lines. but the problem is that when I open a ViewController with a long title it jumps to adjust the title. Following is my code I have tried. I want to avoid jumping (increasing) and show the multiline text as same as it if using single line text. the normal effect.
func multiLineHeader() {
if #available(iOS 11.0, *) {
if let subviews = self.navigationController?.navigationBar.subviews {
for navItem in subviews {
for itemSubView in navItem.subviews {
if let largeLabel = itemSubView as? UILabel {
if largeLabel.canFitInSingleLine() { return }
largeLabel.numberOfLines = 2
largeLabel.lineBreakMode = .byWordWrapping
largeLabel.sizeToFit()
self.navigationController?.navigationBar.frame.size.height += largeLabel.frame.height
self.navigationController?.navigationBar.layoutIfNeeded()
}
}
}
}
}
}
I am calling this from viewDidAppear(). I tried calling from viewDidLoad() and viewWillAppear() does not work event title is not in 2 lines. as it was when called from viewDidAppear().
If there is anything which is not clear, comment and I will add more details.
This is not a solution to solve the multiline jump effect.
My question was based on the multiline title which I saw in the App Store(app) which did not have a jump effect. After a while when I dig deep into it. I found out that even in the App Store there is a jump effect on some screens and in some places App Store is using view rather than navigation bar.
For example, if you go to the App Store and search for Thunderbox entertainment.
1) Go to one of its apps and then click on company name to see all apps you will see a navigation bar with jump effect.
2) If you open the company profiles directly from the search screen you won't see any jump effect.

Is it possible use different instance of UIMenuController for UITextField in Swift?

According to link we should use a singleton UIMenuController instance which is referred to the editing menu.
The problem is I want to show extra items in different situations. For instance, I want to just show "copy" item when keyboard is up. and show "copy" and "reply" when tapping on a tableview row.
When I add "reply" to the UIMenuController instance it is shown when tapping on UITextField too. therefore, I added these codes:
func textViewDidBeginEditing(_ textView: UITextView) {
var nonReplyMenuItems: [UIMenuItem] = []
if let allMenuItems = UIMenuController.shared.menuItems {
for menuItem in allMenuItems {
if menuItem.title != "reply".localized {
nonReplyMenuItems.append(menuItem)
}
}
}
UIMenuController.shared.menuItems = nonReplyMenuItems
UIMenuController.shared.setMenuVisible(true, animated: true)
}
It fixed the problem in most situations, but not all.
when keyboard is up and tapping on a row in tableview "reply" will be added. Then when I tap on UITextView the reply will be shown there too.
It seems your scenario is like it:
tap on textfield ----> shows copy
tap on tableview ---> shows copy and reply
tab on textfield ----> shows copy and reply (you want only copy shows)
As I know the textViewDidBeginEditing calls when your text filed is not editing and you tap on that; So if you have two textfileds by switching on that method calls every time but when you are switching between a text filed and another action base object your text field is editing and its state has not changed.
When you touch on tableview you must call textfield.resignFirstResponder() so when you tap on text field again the textViewDidBeginEditing calls again, the problem of this is hiding keyboard; The better way I preferÙˆ is adding function to touch down of text field or on gesture to do what you write on textViewDidBeginEditing method

How to autofill the right textfield

I need to autocomplete textfields from tableview. How can i select the right textfield(where i am writing at this moment)?
code is here
If I understand correctly you have two UITextFields and a UITableView and when you tap on a cell in the table view you want to put it's text into whichever text field has your input focus.
Try this:
if firstTextField.isFirstResponder {
firstTextField.text = selectedCell.textLabel.text
}
if secondTextField.isFirstResponder {
secondTextField.text = selectedCell.textLabel.text
}
The isFirstResponder method tells you if the control has the current focus and only one of them can.

Swift, UitextField background with image on rightmost side

I have one UItextField and I want to show the date image inside a textfield in the rightmost side.
Below is my code
txtEffectiveDate.rightView = UIImageView(image: UIImage(named:"sorting-selected-22pt" )) // named: "sorting-selected-22pt")
But somehow it is not displaying in the UI page.
Let me know what i am Doing wrong?