Auto showing keyboard in swift - swift

I need to show the keyboard automatically when I will go to a particular View controller. Let take an example I have two view controller named "A" and "B". I have a text field present in the "B" view controller. When I am navigating from "A" to "B", I need to show the keyboard automatically. How to do it?

in B view controller
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
textField.becomeFirstResponder()
}

Type it in your B ViewController
override func viewDidLoad() {
super.viewDidLoad()
self.yourTextFieldName.delegate = self
self.showKeyBoard()
}
func showKeyBoard(){
self.yourTextFieldName.becomeFirstResponder()
}

Related

View Controller shows as pop-up

I want a normal segue, so no pop-up. But when I push the button to go to the next ViewController, it shows as ab pop-up. The buttons to the next ViewController is in a different storyboard than the ViewController I'd like to go to.
This is the code of the first ViewController:
import UIKit
//some other code
class LoginViewController: UIViewController {
//some other code
override func viewDidLoad() {
super.viewDidLoad()
//some other code
}
#objc private func ProceedTapped(){
//Door naar storyboard van de daadwerkelijke app
print("Proceed clicked")
performSegue(withIdentifier: "goToAppIdentifier", sender: self)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
}
And this is the code of the ViewController I'd like to go to:
import UIKit
class HomeScreen: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print("Home has loaded")
}
}
The kind of the segue of the "Storyboard Segue" is "Show (e. g. push)"
You just have to go to story board select the 2nd view controller and change presentation style to "Full Screen". Please check below image for reference

Change variable while dismissing modal controller

EDIT: I have decided to change the way my app works, so this problem is solved. Thanks to everyone who helped!
I have a modal controller where when I press a button it dismisses the view. What I want to do is change a variable in another view controller when I dismiss it, is that possible? Or, if this doesn't work, is there a way for me to access the changed variable of another swift file? I will add my code below:
class PopupViewController: UIViewController {
var event = ""
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func dismiss(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
#IBAction func event910(_ sender: Any) {
event = "storyTime"
dismiss(animated: true, completion: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = segue.destination as! ViewController
vc.event = event
}
}
I want to pass the changed variable "event" to another view controller, how can I do this?
Delegate View Controller is as follows. : -
it is the place where you will send the data to the next swift file
protocol myprotocol {
func anyfunction(_ param1:String)
}
struct mystruct1 {
var delegate:myprotocol?
// where you want tot start the delegate / send the data to the next file
func anymethod(){
delegate.anyfunction(sendTheDataYouWant)
}
}
// it is here you will receive the data
class anyclass:UIViewController ,myprotocol {
let class1 = mystruct1()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
class1.delegate = self
}
func anyfunction(param1:String){
// here Save the data you want
// because this function will be triggered as delegate will be called
}
}
ps:- I reccomend you to read https://docs.swift.org/swift-book/LanguageGuide/Protocols.html
& apple docs

How to implement edit/delete behaviour on an embedded tableView?

So I have an embedded tableview and I want to implement edit/delete behaviour:
In my HomePageViewController I have:
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = editButtonItem
}
However, all that happens is when I click on the edit button, it says done, and the embedded tableview does nothing at all. When I put the above code in the tableview nothing happens.
How do I get the navigation controller/parent view controller to recognise the embedded table view?
It looks like you're using an embed segue to embed a UITableViewController. In your parent view controller, you can do 1 of 2 things easily to achieve you goal.
Method 1: Use the child view controller's edit button
override func viewDidLoad() {
super.viewDidLoad()
// Find and (optionally assign it to a variable for later convenience) the embedded controller, IBOutlets aren't available for VCs embedded within a storyboard
let childControllers = childViewControllers.filter { return $0 is EventTableViewController }
let embeddedController = childControllers[0] as! EventTableViewController
navigationItem.leftBarButtonItem = embeddedController.editButtonItem
}
Method 2: Forward edit events to child view controllers
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = editButtonItem
}
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
// Forward editing state to children
childViewControllers.forEach { $0.setEditing(editing, animated: animated) }
}
Note: editButtonItem was exposed in iOS 10 (but was implemented much earlier). For deployment targets less than iOS 10, you can use a custom edit button combined with method 2.
In this situation, you can't use the automatic behavior of the edit button. It's up to you to implement it yourself. You need to toggle both the isEditing of the table view and the appearance of the button. Here's an example from one of my own apps:
func doEdit(_ sender: Any?) {
var which : UIBarButtonSystemItem
if !self.tableView.isEditing {
self.tableView.setEditing(true, animated:true)
which = .done
} else {
self.tableView.setEditing(false, animated:true)
which = .edit
}
let b = UIBarButtonItem(barButtonSystemItem: which,
target: self, action: #selector(doEdit))
self.navigationItem.rightBarButtonItem = b
}

interactivePopGestureRecognizer corrupts navigation stack on root view controller

In my UINavigationController I added custom back buttons with the side effect that it is not possible anymore to swipe left to right to pop the view controller and navigate back.
So I implemented interactivePopGestureRecognizer in my custom UINavigationController class:
class UINavigationControllerExtended: UINavigationController, UIGestureRecognizerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
if self.respondsToSelector(Selector("interactivePopGestureRecognizer")) {
self.interactivePopGestureRecognizer?.delegate = self
}
}
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailByGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return gestureRecognizer.isKindOfClass(UIScreenEdgePanGestureRecognizer)
}
}
This works fine except when I am in my root view controller (RVC) which is a UICollectionViewController, i.e. the most bottom view controller in the navigation stack. When I do the swipe left to right gesture, nothing seems to happen, as expected. But when I then tap a UICollectionViewCell the destination view controller (DVC) does not get pushed over the RVC. Instead I only see the DVC's shadow on the right side of the screen.
The RVC is not responsive anymore, but as I swipe left to right again, the DVC interactively moves right to left into the screen. When I finish the gesture, the DVC moves completely into the screen just to quickly disappear left to right again. The RVC becomes responsive again.
So it seems the DVC gets pushed onto the navigation stack but not visibly into the screen.
Any suggestions where this strange behaviour originates?
Implement UINavigationControllerDelegate for your navigation controller and enable/disable the gesture recognizer there.
// Fix bug when pop gesture is enabled for the root controller
func navigationController(navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool) {
self.interactivePopGestureRecognizer?.enabled = self.viewControllers.count > 1
}
Keeping the code independent from the pushed view controllers.
My current solution is to disable the interactivePopGestureRecognizer in the root view controller:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.navigationController?.interactivePopGestureRecognizer?.enabled = false
}
In the first child view controller I enable it again. But this seems to be more a workaround because I don't understand the actual problem why the navigation stack got messed up in the first place.
Here is my solution for this. Swift 4.2
Implement UIGestureRecognizerDelegate and UINavigationControllerDelegate protocols. In my case I did this in an UITabBarController that would be also my root view controller.
Then, on viewDidLoad, do:
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.interactivePopGestureRecognizer?.delegate = self
self.navigationController?.delegate = self
}
Then, add the delegate method for UINavigationControllerDelegate and check if it is the root view by counting the number of view on the navigation controller and disable it if it is or enable it if its not.
func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
let enable = self.navigationController?.viewControllers.count ?? 0 > 1
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = enable
}
Lastly, add the UIGestureRecognizerDelegate method
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
This should fix the problem without the necessity of manually enabling/disabling the gesture recogniser in every view of your project.
func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
self.interactivePopGestureRecognizer?.isEnabled = self.viewControllers.count > 1
}
Updated #rivera solution for Swift 5
Solution from Guilherme Carvalho worked for me, but I had to assign delegate methods in viewWillAppear, in viewDidLoad was too late for my implementation.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.interactivePopGestureRecognizer?.delegate = self
navigationController?.delegate = self
}
Try this code
func navigationController(navigationController: UINavigationController, didShowViewController vc: UIViewController, animated: Bool) {
self.interactivePopGestureRecognizer?.enabled = self.vc.count > 1
}

Navigation Controllers in Swift

I have embedded my story board view controllers with the navigation controller. However, I want one view specific controller to not have the navigation bar. How can I do this?
Hide the navigation bar in viewWillAppear.
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBarHidden = true
}
Also, make sure you have it appear again when you leave that particular view controller. I usually do this in viewWillDisappear.
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.navigationBarHidden = false
}