NSNotificationCenter. Send data from one to others ViewControllers Swift - ios

I'm trying to show some data in a LastVC depending of the choose of the user in each view. I Let you the image of our App
I am using NSNotificationCenter but the labels in the lastVC do not change.
What is wrong?
#IBOutlet weak var colorLabelSelected: UILabel!
#IBOutlet weak var nextOutlet: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
nextOutlet.hidden = true
}
#IBAction func greenButton(sender: AnyObject) {
NSNotificationCenter.defaultCenter().postNotificationName("setGreenColorID", object: nil)
colorLabelSelected.text = "You have selected a green BG color"
nextOutlet.hidden = false
}
#IBAction func blueButton(sender: AnyObject) {
NSNotificationCenter.defaultCenter().postNotificationName("setGreenColorID", object: nil)
colorLabelSelected.text = "You have selected a blue BG color"
nextOutlet.hidden = false
}
#IBAction func pinkButton(sender: AnyObject) {
NSNotificationCenter.defaultCenter().postNotificationName("setGreenColorID", object: nil)
colorLabelSelected.text = "You have selected a pink BG color"
nextOutlet.hidden = false
}
}
class ViewController2: UIViewController {
#IBOutlet weak var nextOutlet: UIButton!
#IBOutlet weak var titleLabelSelected: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
nextOutlet.hidden = true
}
#IBAction func newsButton(sender: AnyObject) {
NSNotificationCenter.defaultCenter().postNotificationName("setNewsThemeID", object: nil)
titleLabelSelected.text = "You have selected News as title"
nextOutlet.hidden = false
}
#IBAction func sportButton(sender: AnyObject) {
NSNotificationCenter.defaultCenter().postNotificationName("setSportThemeID", object: nil)
titleLabelSelected.text = "You have selected Sport as title"
nextOutlet.hidden = false
}
#IBAction func wealthButton(sender: AnyObject) {
NSNotificationCenter.defaultCenter().postNotificationName("setWealthThemeID", object: nil)
titleLabelSelected.text = "You have selected Wealth as title"
nextOutlet.hidden = false
}
}
The same with VC3
class lastViewController: UIViewController {
#IBOutlet weak var finalColorLabel: UILabel!
#IBOutlet weak var finalThemeLabel: UILabel!
#IBOutlet weak var finalRelevanceLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "setBlueColor:", name: "setBlueColorID", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "setGreenColor:", name: "setGreenColorID", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "setPinkColor:", name: "setPinkColorID", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "setNewsTheme:", name: "setNewsThemeID", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "setSportTheme:", name: "setSportThemeID", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "setWealthTheme:", name: "setWealthThemeID", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "setTenPoints:", name: "setTenPointsID", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "setSevenPoints:", name: "setSevenPointsID", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "setFivePoints:", name: "setFivePointsID", object: nil)
}
//setColors
func setBlueColor(notification: NSNotification) {
finalColorLabel.text = "blue"
}
func setGreenColor(notification: NSNotification) {
finalColorLabel.text = "green"
}
func setPinkColor(notification: NSNotification) {
finalColorLabel.text = "pink"
}
//setTheme
func setNewsTheme(notification: NSNotification) {
finalThemeLabel.text = "news"
}
func setSportTheme(notification: NSNotification) {
finalThemeLabel.text = "sport"
}
func setWealthTheme(notification: NSNotification) {
finalThemeLabel.text = "wealth"
}
//setRelevance
func setTenPoints(notification: NSNotification){
finalRelevanceLabel.text = "ten"
}
func setSevenPoints(notification: NSNotification){
finalRelevanceLabel.text = "seven"
}
func setFivePoints(notification: NSNotification){
finalRelevanceLabel.text = "five"
}

Are you sure the lastVC even exist when you post notifications from the other ViewControllers? If you segue to it as a part of a normal show segue the view controller get's recreated every time you segue to it.
If you want to pass data from one viewController to the other you should probably do it in prepareForSegue. I'd approach it this way
1) create an object:
struct userSettings {
var backgroundColor: NSColor?
var title: String?
var relevance: Int?
}
2) fill appropriate field in each ViewController
3) pass it in prepare for segue
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let identifier = segue.identifier {
switch identifier {
case segueToController:
if let viewController = segue.destinationViewController as? myControllerClass {
viewController.userSettings = userSettings
}
default: break
}
}
}

Related

How to Change an image in 2nd View Controller using a Button in 1st View Controller

I have stetted up switch in my First View Controller and assigned the following action to it :-
class ThirdViewController: UIViewController {
#IBOutlet weak var Image: UIImageView!
#IBOutlet weak var playerNum1Button: UIButton!
#IBOutlet weak var toggleSwitch: UISwitch!
override func viewDidLoad() {
super.viewDidLoad()
self.toggleSwitch.setOn(UserDefaults.standard.bool(forKey: "toggleState"), animated: true)
}
#IBAction func numPlayers1(_ sender: Any) {
performSegue(withIdentifier: "3to8segue", sender: self)
}
IBAction func toggleSwitch(_ sender: UISwitch) {
if (toggleSwitch.isOn == true) {
Image.image = UIImage(named: "Night")
}
else {
Image.image = UIImage(named: "background")
}
UserDefaults.standard.set(sender.isOn, forKey: "toggleState")
}
It is Able to change the image in the First View Controller. But, how can I get that switch to change the image in 2nd View controller as well when it is turned on? Thanks for the help!
Using Notification Center
Example:
In FirstViewController:
#IBAction func onPressButton(_ sender: UIButton) {
UserDefaults.standard.set(sender.isSelected, forKey: "pressedButton")
NotificationCenter.default.post(name: "changeImageBackground", object: nil)
}
In SecondViewController:
NotificationCenter.default.addObserver(target: self, selector: #selector(didChangeImageBackground), name: "changeImageBackground", object: nil)
#objc func didChangeImageBackground() {
let changed = UserDefaults.standard.bool(forKey: "pressedButton")
if changed {
// image when pressed
} else {
// image when haven't pressed
}
}

Data transfer between tab view controllers - swift

There are 3 child tab view controllers. There are labels as 0 in view controllers. If the number(0) of labels increases in any view controller, I want to increase from the others. How can i do this data transfer.
class tab1Controller: UIViewController {
#IBOutlet weak var countLabel: UILabel!
var count = ""
override func viewDidLoad() {
super.viewDidLoad()
count = countLabel.text!
UserDefaults.standard.set(count, forKey: "count")
UserDefaults.standard.synchronize()
}
class tab2Controller: UIViewController {
#IBOutlet weak var countLabel2: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
countLabel2.text = UserDefaults.standard.string(forKey: "count")
}
I did something like this but it didn't work
I think the simplest way in your case is to update the label text in viewWillAppear method.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
countLabel.text = UserDefaults.standard.string(forKey: "count")
}
You also should update the value in UserDefaults every time the number changes.
Do it i viewWillAppear() instead of viewDidLoad().. Because DidLoad() of all controllers called when tabBar construct and show first tab ...
#IBOutlet weak var countLabel2: UILabel!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
countLabel.text = UserDefaults.standard.string(forKey: "count")
}
I assume that your child controllers are created at the same when the tab controller created. In such cases that needed to notify other existing controllers, you must use NotificationCenter.
A notification dispatch mechanism that enables the broadcast of information to registered observers.
extension Notification.Name {
static let didReceiveCountData = Notification.Name("didReceiveCountData")
}
class tab1Controller: UIViewController {
#IBOutlet weak var countLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Listen notifications for name .didReceiveCountData.
// onDidReceiveCountData(_:) will be called when notification received.
NotificationCenter.default.addObserver(self, selector: #selector(onDidReceiveCountData(_:)), name: .didReceiveCountData, object: nil)
}
// This will be called when the count changes.
#objc func onDidReceiveCountData(_ notification:Notification) {
if let newCount = notification.object as? String {
countLabel.text = newCount
}
}
// Call this when you need to change count and notify other tabs.
private func changeCount(_ newCount: String) {
NotificationCenter.default.post(name: .didReceiveCountData, object: newCount)
}
}
class tab2Controller: UIViewController {
#IBOutlet weak var countLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Listen notifications for name .didReceiveCountData.
// onDidReceiveCountData(_:) will be called when notification received.
NotificationCenter.default.addObserver(self, selector: #selector(onDidReceiveCountData(_:)), name: .didReceiveCountData, object: nil)
}
// This will be called when the count changes.
#objc func onDidReceiveCountData(_ notification:Notification) {
if let newCount = notification.object as? String {
countLabel.text = newCount
}
}
// Call this when you need to change count and notify other tabs.
private func changeCount(_ newCount: String) {
NotificationCenter.default.post(name: .didReceiveCountData, object: newCount)
}
}

SWIFT4: How to have two tap gestureRecognizers work together

NB: My gesture recognizers are not different and are in different viewControllers.
TLDR: How can I do to have both my tap gesture recognisers to work?
I have created a side menu from some container views and I added to it a gesture recognizer that allows me to dismiss it on tap. Then I created another ViewController where i have some textfields. Since I wanted the keyboard to dismiss on tap I even added a gesture recognizer that allows me to hide the keyboard whenever i tap the view. Now I noticed that having the gesture that hides the keyboard doesn't let my side menu hide on tap:
SIDE MENU VIEW CONTROLLER
#IBOutlet weak var bigContainer: UIView!
#IBOutlet weak var sideMenuConstraint: NSLayoutConstraint!
var sideMenuOpen = false
var gesture : UITapGestureRecognizer?
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self,
selector: #selector(toggleSideMenu), name: NSNotification.Name("ToggleSideMenu"), object: nil)
gesture = UITapGestureRecognizer(target: self, action: #selector(ContainerViewController.toggleSideMenu))
}
#objc func toggleSideMenu() {
if sideMenuOpen {
sideMenuOpen = false
sideMenuConstraint.constant = -240
self.bigContainer.removeGestureRecognizer(gesture!)
} else {
sideMenuOpen = true
sideMenuConstraint.constant = 0
self.bigContainer.addGestureRecognizer(gesture!)
}
}
MAIN VIEW VIEW CONTROLLER
#IBOutlet weak var textField: UITextField!
#IBOutlet weak var menuOutlet: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
let endEditingTapGesture = UITapGestureRecognizer(target: view, action: #selector(UIView.endEditing(_:)))
endEditingTapGesture.cancelsTouchesInView = false
view.addGestureRecognizer(endEditingTapGesture)
}
#IBAction func toggleSideMenu(_ sender: Any) {
print("Toggle side menu")
NotificationCenter.default.post(name: NSNotification.Name("ToggleSideMenu"), object: nil)
}
}
I tried removing the tap gesture to hide the keyboard in my toggleSideMenu button but it didn't work.
In your MAIN VIEW CONTROLLER Replace with below code:
var endEditingTapGesture:UIGestureRecognizer!
override func viewDidLoad() {
super.viewDidLoad()
endEditingTapGesture = UITapGestureRecognizer(target: view, action: #selector(UIView.endEditing(_:)))
endEditingTapGesture.cancelsTouchesInView = false
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notication:)), name: UIResponder.keyboardWillHideNotification, object: nil) //Add keyboard notification
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notication:)), name: UIResponder.keyboardWillShowNotification, object: nil)
}
#IBAction func toggleSideMenu(_ sender: Any) {
print("Toggle side menu")
NotificationCenter.default.post(name: NSNotification.Name("ToggleSideMenu"), object: nil)
}
//Keyboard method
#objc func keyboardWillHide(notication:Notification) {
view.removeGestureRecognizer(endEditingTapGesture)
}
#objc func keyboardWillShow(notication:Notification) {
view.addGestureRecognizer(endEditingTapGesture)
}

This keyboard won't leave

I'm having a weird behavior from a keyboard. But only on one of the view controllers. I have another view controller with a UITextField and that one works charms (as soon as I press the return button it disappears). And the weird part is that that one isnt even setup at all. it only has becomefirstresponder on viewdidload and resignfirstresponder on viewwilldissapear.
THIS keyboard however, VIDEO OF NASTY KEYBOARD is quite the nasty booger.
NOTE: In the video I do the following:
* Tap the UITextfield
* Keyboard Appears (view shifts up)
* I type lol and then press send (nothing happens)
* I start tapping frantically all around the screen (except the UITextField)
* Keyboard dissapears
Here's the code for this View:
class ChatScreenVC: UIViewController, UITextFieldDelegate {
var gameDelegate: GameManagerDelegate?
var chatLogString: String = ""
var offsetY:CGFloat = 0
#IBOutlet weak var chatView: UIView!
#IBOutlet weak var closeBtn: UIButton!
#IBOutlet weak var titleBtn: UILabel!
#IBOutlet weak var chatLog: UITextView!
#IBOutlet weak var chatInput: UITextField!
#IBOutlet weak var sendBtn: UIButton!
#IBOutlet weak var viewCenterConstraint: NSLayoutConstraint!
#IBAction func inputTapped(_ sender: Any) {
chatInput.becomeFirstResponder()
}
#IBAction func closeTapped(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
#IBAction func sendTapped(_ sender: Any) {
if chatInput.text != nil && chatInput.text != "" && chatInput.text != " " {
gameDelegate?.sendChat(message: chatInput.text!)
}
chatInput.text = ""
chatInput.resignFirstResponder()
self.view.endEditing(true)
}
#objc func keyboardWillShow(_ notification: Notification) {
if let keyboardFrame: NSValue = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue {
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height
viewCenterConstraint.constant = -keyboardHeight
}
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
print("THIS IS HAPPENING!")
chatInput.resignFirstResponder()
self.view.endEditing(true)
return true
}
#objc func keyboardWillHide(_ notification: Notification) {
if let keyboardFrame: NSValue = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue {
viewCenterConstraint.constant = 0
}
}
#objc func reloadData() {
chatLogString = (gameDelegate?.updateChatString())!
chatLog.text = chatLogString
}
override func viewDidLoad() {
super.viewDidLoad()
chatLog.text = chatLogString
chatLog.isEditable = false
chatInput.autocorrectionType = .no
// Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool) {
setupView()
animateView()
}
override func viewWillDisappear(_ animated: Bool) {
chatInput.resignFirstResponder()
}
override func viewDidAppear(_ animated: Bool) {
NotificationCenter.default.addObserver(self, selector: #selector(self.reloadData), name: Notification.Name(rawValue: SMACK_TALK), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
override func viewDidDisappear(_ animated: Bool) {
NotificationCenter.default.removeObserver(self, name: Notification.Name(rawValue: SMACK_TALK), object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
}
}
Now before you go and tell me "not to use both the resignfirstresponder and the endediting(true)" I gotta clear out that I tried both individually and together and still don't get any response.
You can resign keyboard before sendChat(). I think it is sync call and it will block main thread for sometimes thats why this weird behaviour occurs.
chatInput.resignFirstResponder() before you are making API call.
#IBAction func sendTapped(_ sender: Any) {
if chatInput.text != nil && chatInput.text != "" && chatInput.text != " " {
chatInput.resignFirstResponder()
self.view.endEditing(true)
gameDelegate?.sendChat(message: chatInput.text!)
}
}

Keyboard move up and down

I have a loginView, inside that i have two textFields and a button. I have to move up the view while tapping on textField and move down the view when press the return key.
My problem is that it is working fine for all conditions but while tap on the one textField view is moving up but at same time when we go for next textField view is moving down.
import UIKit
class CheckFontIconView: UIViewController,UITextFieldDelegate {
var activeField: UITextField?
#IBOutlet weak var loginFieldsView: UIView!
#IBOutlet weak var label: UILabel!
#IBOutlet weak var mobileNo: UITextField!
#IBOutlet weak var textFieldPassword: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
mobileNo.delegate = self
textFieldPassword.delegate = self
registerForKeyboardNotifications()
}
#IBAction func btnLoginAction(_ sender: Any) {
}
deinit {
//NotificationCenter.default.removeObserver(self)
self.deregisterFromKeyboardNotifications()
}
func registerForKeyboardNotifications() {
//Adding notifies on keyboard appearing
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func deregisterFromKeyboardNotifications() {
//Removing notifies on keyboard appearing
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func keyboardWasShown(notification: NSNotification) {
//Need to calculate keyboard exact size due to Apple suggestions
var info = notification.userInfo!
let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
if self.activeField != nil {
self.loginFieldsView.frame.origin.y -= (keyboardSize?.height)!
}
}
func keyboardWillBeHidden(notification: NSNotification) {
//Once keyboard disappears, restore original positions
var info = notification.userInfo!
let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
self.loginFieldsView.frame.origin.y -= (keyboardSize?.height)!
self.loginFieldsView.endEditing(true)
}
func textFieldDidBeginEditing(_ textField: UITextField) {
self.activeField = textField
}
func textFieldDidEndEditing(_ textField: UITextField) {
self.activeField = nil
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
activeField?.resignFirstResponder()
return true
}
}
Once try with scrollView,I hope it will help you.
Use a scroll view to moving text field up and down.
Making Up
Make a #IBOutlet weak var scroller: UIScrollView!;
Under textFieldDidBeginEditing:textField method
Create a CGPoint with x: 0 and y texfield.frame.origin.y
And then start moving the scroller with setContentOffset:animated function.(Parameter will be your CGPoint and a boolean value true).
Making Down
Under textFieldDidEndEditing:textField set your scroller to CGPoint.zero with same setContentOffset:animated function.
*And your textFieldShouldReturn:textField should be resignFirstResponder or to next textField.
Please let me know if having any problem on this.
Thanks for ask question...
Use TPKeyboard to achieve above functionality then no manage this kind of stuff (Automatically manage by TPKeyboard)...
Update:
I think you have done some mistake in given below code...
func textFieldDidEndEditing(_ textField: UITextField) {
self.activeField = nil
}
You have to DEBUG and identify exact issue..
Happy coding...
I made the simple code for your problem. It is not good but you can refer with that idea for fix your problem.
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
var activeField: UITextField?
#IBOutlet weak var loginFieldsView: UIView!
#IBOutlet weak var label: UILabel!
#IBOutlet weak var mobileNo: UITextField!
#IBOutlet weak var textFieldPassword: UITextField!
var originalHeight:CGFloat!
override func viewDidLoad() {
super.viewDidLoad()
mobileNo.delegate = self
textFieldPassword.delegate = self
originalHeight = self.loginFieldsView.frame.origin.y
registerForKeyboardNotifications()
}
#IBAction func btnLoginAction(_ sender: Any) {
}
deinit {
//NotificationCenter.default.removeObserver(self)
self.deregisterFromKeyboardNotifications()
}
func registerForKeyboardNotifications() {
//Adding notifies on keyboard appearing
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func deregisterFromKeyboardNotifications() {
//Removing notifies on keyboard appearing
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func keyboardWasShown(notification: NSNotification) {
//Need to calculate keyboard exact size due to Apple suggestions
var info = notification.userInfo!
let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
if self.activeField != nil {
if self.loginFieldsView.frame.origin.y == originalHeight {
self.loginFieldsView.frame.origin.y -= (keyboardSize?.height)!
}
}
}
func keyboardWillBeHidden(notification: NSNotification) {
//Once keyboard disappears, restore original positions
var info = notification.userInfo!
let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
if originalHeight > self.loginFieldsView.frame.origin.y {
self.loginFieldsView.frame.origin.y += (keyboardSize?.height)!
}
self.loginFieldsView.endEditing(true)
}
func textFieldDidBeginEditing(_ textField: UITextField) {
self.activeField = textField
}
func textFieldDidEndEditing(_ textField: UITextField) {
self.activeField = nil
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
activeField?.resignFirstResponder()
return true
}
}
Just one change hold your view old frame in a variable and assign it again to your view when keyboard disappear.
import UIKit
class CheckFontIconView: UIViewController,UITextFieldDelegate {
var activeField: UITextField?
#IBOutlet weak var loginFieldsView: UIView!
#IBOutlet weak var label: UILabel!
#IBOutlet weak var mobileNo: UITextField!
#IBOutlet weak var textFieldPassword: UITextField!
var previousFrame : CGRect!
override func viewDidLoad() {
super.viewDidLoad()
previousFrame=self.loginFieldsView.frame
mobileNo.delegate = self
textFieldPassword.delegate = self
registerForKeyboardNotifications()
}
func keyboardWasShown(notification: NSNotification) {
//Need to calculate keyboard exact size due to Apple suggestions
var info = notification.userInfo!
let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
if self.activeField != nil {
self.loginFieldsView.frame.origin.y = previousFrame.origin.y-(keyboardSize?.height)!
}
}
func keyboardWillBeHidden(notification: NSNotification) {
//Once keyboard disappears, restore original positions
/*var info = notification.userInfo!
let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size*/
self.loginFieldsView.frame = previousFrame
//self.loginFieldsView.endEditing(true)
}
func textFieldDidBeginEditing(_ textField: UITextField) {
self.activeField = textField
}
func textFieldDidEndEditing(_ textField: UITextField) {
self.activeField = nil
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
activeField?.resignFirstResponder()
return true
}
}

Resources