Automatically select a textfield upon loading new VC - ios

Im sure this is simple but I can't figure it out so I thought I'd ask! I have a tab view controller with 5 different tabs on it. On one of them, when somebody clicks on it, I want the cursor to be automatically selected in the textfield so they can start typing right away. How can I do this in Swift?

You can select a text field with
textfield.becomeFirstResponder()
You'd probably want to put that in you viewWillAppear or viewDidAppear
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
textField.becomeFirstResponder()
}

Related

How do you refresh a table view once dismissing a View Controller after inserting values using SQLite3?

I have a tableViewController that holds a list of activities. Theres a button that takes you to a new view controller, which allows you to create a new activity, that then gets inserted into the database when the save button is pressed, which also dismisses the view controller to take you back to the list of activities. I want the activities to refresh so that the new activity they just inserted will be added to the table. I have tried
nextViewController = ActivitiesViewController()
dismissVC(){
nextViewController.viewDidLoad() }
It currently takes you back to the correct table view, but the rows do not update. It isn't until the phone is restarted that the values update.
Anyone have any ideas on how to fix this?
Thanks
You probably want to override the method viewWillAppear(_ animated: Bool) in your ActivitiesViewController and call a function to update the data (just as you do in viewDidLoad).
class ActivitiesViewController: UITableViewController { // UITableViewController or whatever you're using
// your stuffs
// ...
// ...
override func viewDidLoad() {
super.viewDidLoad()
// load datas
tableView.reloadData()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// load datas
tableView.reloadData()
}
}
To explain a bit further why you should do this; in your code snippet, you're creating a new instance of ActivitiesViewController that is not the one that is displayed on the screen. Even if you update it, it's not linked to the one that is displayed.

How to automatically reload the data from viewcontroller?

I am trying to find the solution for how to automatically reload all the data in view controller but i can't find any. Right now i am using push to refresh to reload the button/title/data in view controller viewDidload() but i want to have it automalliy reload everything every time i come back to this viewcontroller. For example when app lunched, it load view controller A then i clicked on the button to go View controller B but i want to controller A to refresh everything after back from B so how can i do that?
Thanks
For this, there is a method viewWillAppear
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
reloadData()
}
func reloadData() {
//All you need to update
}

Keyboard does not disappear after viewDidDisappear

iOS 11.2, Xcode 9.2
I've tried all the ways to dismiss keyboard on viewDidDisappear of the old UIViewController AFTER a new UIViewController is pushed into UINavigationController stack. But with no luck.
If I dismiss it on viewWillDisappear - it will be dismissed but with animation DURING push animation. It is not the desired behaviour. I want the old UIViewController's keyboard to be dismissed only when the controller is no longer visible.
The behavior should be like in Telegram app:
In any dialog with visible keyboard press on opponents avatar and you'll be pushed to opponents account information. Then, if you press back button, you'll be redirected back to a dialog. But the keyboard will be already dismissed.
Any help is appreciated!
P.S. The question might look like a duplicate, but I have failed to make it work with the solutions I found.
Edit 1.
I have created a small TEST PROJECT which represents a failure to achieve the desired behavior.
In order to reproduce the undesired behavior:
Launch the app.
Tap on UITextField or UITextView and wait for the keyboard to appear.
Tap on "Next" button and wait for a new controller to be pushed.
Tap on "Back" button and wait for a new controller to be popped.
As a result - the initial view controller will have the active keyboard after the push/pop actions. I need the keyboard to be hidden after the push/pop actions. Also, the keyboard should not be dismissed before the initial view controller becomes invisible, it should be dismissed after viewDidDisappear action.
There are cases where no text field is the first responder but the keyboard is on screen. In these cases, the above methods fail to dismiss the keyboard.
Use the property: isEditable of your textView. Here is a tested code:
override func viewWillAppear(_ animated: Bool) {
self.viewTextView.isEditable = false
super.viewWillAppear(animated)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.viewTextView.isEditable = true
}
Results:
Comments from #iWheelBuy:
Sometimes, text views will have inputAccessoryView. The way you do it
will make the keyboard disappear, but the inputAccessoryView will
remain... That is why you should also make inputAccessoryView = nil
or inputAccessoryView = UIView() when setting isEditable = false
The problem happens because responders are managed (restored, saved) by UIKit between viewWillAppear and viewDidAppear, just before view has appeared, and between viewWillDisapear: and viewDidDisapear:, just before view has disappeared. That is why any change made to responders is visible during animation.
Instead of removing responders, to get the effect you want, you can prevent views from becoming responders before view appears again.
The simplest way to do this for UITextField and UITextView is to temporary disable interaction just before view will appear, and then restore it after the view did reappeared.
override func viewWillAppear(_ animated: Bool) {
self.viewTextField.isUserInteractionEnabled = false
self.viewTextView.isUserInteractionEnabled = false
super.viewWillAppear(animated)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.viewTextField.isUserInteractionEnabled = true
self.viewTextView.isUserInteractionEnabled = true
}
This will give you the same effect Telegram has.

iOS Navigation Bar Title set dinamically is making a ellipsis in text when view appears

I was trying to set the back button title in a navigation bar, like this
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.title = self.backUpTitle
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
self.title = "Back"
}
Where self.backUpTitle has the original title for the current ViewController.
It works very well, but I'm a having a quick effect each time I click "Back": the title of the navigation bar appears with the first three letters followed by ellipsis (Eg: "Title" would show as "Tit..."), and after the view fully appears, it shows the entire title without any problem.
The thing is... it does not happen in a normal case, so I guess it has to do with my solution about setting Back Button Title.
The question is: is there a way to avoid this effect? Am I calling self.title in a wrong function?
I’m using Xcode 8 and iOS 10.0
I’ve tried running your code on my own machine and I’m not showing the same problem; I’m thinking you might be using custom views for the title of the navigation bar and your self.backUpTitle is inside a custom view that causes the ellipsis.
Some suggestions:
If you just want to show “Create User” that way without the ellipsis, you might want to remove all custom views for your navigation bar and just set the ViewController title like what you are doing in your code.
Using “self.title” will change the title of your ViewController, make sure your ViewController is embedded to a UIViewController. However, if you created your navigation bar, setting the title should be:
navigationBar.topItem.title = “Create User”
Just to reiterate, this is what my code looks like (which looks like yours) under a ViewController that is embedded in a UINavigationController:
var backUpTitle: String!
override func viewDidLoad() {
backUpTitle = "Create User"
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.title = self.backUpTitle
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.title = "Back"
}

Back Bar Button Segue hides Toolbar

I am using a Show segue in my application.
Whenever I segue to another screen and press the back bar button, my navigationController.toolbar disappears.
I tried to get rid of it with
navigationController?.toolbar.hidden = false
in my viewDidLoad().
It doesn't work though. Any ideas?
Please add the code in the viewWillAppear() and it should solve the problem you are facing.
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
navigationController?.toolbarHidden = false
}
Remember that viewDidLoad() fires only once during the life cycle of a view controller and in your case , it is in the navigation stack which means it has been already used for that view controller and now when you press back button, it does not work again.
navigationController?.toolbarHidden = false

Resources