I have an identical question to this one , but since I'm new to programming and only really know swift I was wondering if someone could give me its equivalent in swift. Or point me to another question that I may have missed that is in swift.
Thanks!
UPDATE: here's the basic jist of my view controller after I've cut some of the fat away to deal with only the relevant topic. To restate the problem. Not until I have clicked my 'doneButton' to run the createClient() function and navigate back to the client page to edit the freshly created client will the the tap gesture work to dismiss the keyboard.
import UIKit
import CoreData
import Foundation
class NewClientTableViewController: UITableViewController, UINavigationControllerDelegate, UITextFieldDelegate {
let managedObjectContext = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext
#IBOutlet weak var nameTxt: UITextField!
#IBOutlet weak var ageTxt: UITextField!
#IBOutlet weak var phoneTxt: UITextField!
#IBOutlet weak var emailTxt: UITextField!
#IBOutlet weak var heightTxt: UITextField!
#IBOutlet weak var notesTxt: UITextView!
var client: Client? = nil
override func viewDidLoad() {
super.viewDidLoad()
if client != nil {
nameTxt.text = client?.name
ageTxt.text = client?.age
heightTxt.text = client?.height
phoneTxt.text = client?.phone
emailTxt.text = client?.email
notesTxt.text = client?.notes
self.title = client?.name
phoneTxt.delegate = self
let tapGesture = UITapGestureRecognizer(target: self, action: Selector("hideKeyboard"))
tapGesture.cancelsTouchesInView = true
tableView.addGestureRecognizer(tapGesture)
}
}
func hideKeyboard() {
tableView.endEditing(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func doneButton(sender: AnyObject) {
if client != nil {
editClient()
} else {
createClient()
}
dismissViewController()
}
func editClient() {
client?.name = nameTxt.text
client?.age = ageTxt.text
client?.height = heightTxt.text
client?.phone = phoneTxt.text
client?.email = emailTxt.text
client?.notes = notesTxt.text
client?.clientImage = UIImageJPEGRepresentation(contactImage.image, 1)
managedObjectContext?.save(nil)
}
func createClient() {
let entityDescription = NSEntityDescription.entityForName("Client", inManagedObjectContext: managedObjectContext!)
let client = Client(entity: entityDescription!, insertIntoManagedObjectContext: managedObjectContext)
if nameTxt.text == "" {
client.name = "Untitled Client"
} else {
client.name = nameTxt.text
}
client.age = ageTxt.text
client.height = heightTxt.text
client.phone = phoneTxt.text
client.email = emailTxt.text
client.notes = notesTxt.text
client.clientImage = UIImageJPEGRepresentation(contactImage.image, 1)
managedObjectContext?.save(nil)
}
func dismissViewController() {
navigationController?.popToRootViewControllerAnimated(true)
}
}
You can also do it from Storyboard:
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: Selector("hideKeyboard"))
tapGesture.cancelsTouchesInView = true
tableView.addGestureRecognizer(tapGesture)
}
func hideKeyboard() {
tableView.endEditing(true)
}
Translating Objective-C code to Swift is not really hard. You just require a basic knowledge in both languages. If you're new to programming I guess you should familiarise with yourself with the basics first.
As #Ben already recommended, you can also do this programmatically.
Just put this in your viewDidLoad()
tableView.keyboardDismissMode = .onDrag
Swift 4:
On ViewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
self.yourTableView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:))))
}
On corresponding function:
#objc func handleTap(_ sender: UITapGestureRecognizer) {
if sender.state == .ended {
// Do your thang here!
self.view.endEditing(true)
for textField in self.view.subviews where textField is UITextField {
textField.resignFirstResponder()
}
}
sender.cancelsTouchesInView = false
}
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: Selector("hideKeyboard"))
tapGesture.cancelsTouchesInView = true
self.view.addGestureRecognizer(tapGesture)
}
func hideKeyboard()
{
self.view.endEditing(true)
}
this will work fine compared to the above which didnt work for me
For some reason, #Isuru's answer crashes in my app (XCode 7.2.1), and thankfully it's just a minor change - just remove the Selector() call:
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: "hideKeyboard")
tapGesture.cancelsTouchesInView = true
tableView.addGestureRecognizer(tapGesture)
}
func hideKeyboard() {
tableView.endEditing(true)
}
Accepted answer doesn't work for me on XCode 13.1, so I changed the code a little bit:
override func viewDidLoad() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.hideKeyboard))
tapGesture.cancelsTouchesInView = true
tableView.addGestureRecognizer(tapGesture)
}
#objc func hideKeyboard() {
tableView.endEditing(true)
}
Related
I have created one popView with textfield and button in ViewController. if i click button then popView is appearing, and i am able to enter text in textfield and submit is working, and if i tap anywhere in view also i am able to remove popView, but here i want if i tap on anywhere in popView i don't want to dismiss popView, Please help me in the code.
here is my code:
import UIKit
class PopUPViewController: UIViewController {
#IBOutlet weak var popView: UIView!
#IBOutlet weak var inputField: UITextField!
#IBOutlet weak var textLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
popView.isHidden = true
// Do any additional setup after loading the view.
}
#IBAction func butnAct(_ sender: Any) {
view?.backgroundColor = UIColor(white: 1, alpha: 0.9)
popView.isHidden = false
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(PopUPViewController.dismissView))
view.addGestureRecognizer(tap)
}
#objc func dismissView() {
self.popView.isHidden = true
view?.backgroundColor = .white
}
#IBAction func sendButton(_ sender: Any) {
self.textLabel.text = inputField.text
}
}
In my code if i tap anywhere in the view popView is removing even if i tap on popView also its removing, i don't need that, if i tap on popView then popView need not to be remove.
Please help me in the code
You can override the touchesBegan method which is triggered when a new touch is detected in a view or window. By using this method you can check a specific view is touched or not.
Try like this
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
if touch?.view != self.popView {
dismissView()
}
}
func dismissView() {
self.popView.isHidden = true
view?.backgroundColor = .white
}
It's not the way I would have architected this, but to get around the problem you face you need to adapt your dismissView method so that it only dismisses the view if the tap is outside the popView.
To do this modify your selector to include the sender (the UITapGestureRecogniser )as a parameter:
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(PopUPViewController.dismissView(_:)))
and then in your function accept that parameter and test whether the tap is inside your view, and if so don't dismiss the view:
#objc func dismissView(_ sender: UITapGestureRegognizer) {
let tapPoint = sender.location(in: self.popView)
if self.popView.point(inside: tapPoint, with: nil)) == false {
self.popView.isHidden = true
view?.backgroundColor = .white
}
}
Your Popup view is inside the parent view of viewcontroller that's why on tap of popview also your popview is getting hidden.
So to avoid just add a view in background and name it bgView or anything what you want and replace it with view. And it will work fine .
Code:
#IBOutlet weak var bgView: UIView!//Add this new outlet
#IBOutlet weak var popView: UIView!
#IBOutlet weak var inputField: UITextField!
#IBOutlet weak var textLabel: UILabel!
#IBOutlet weak var submitButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
popView.isHidden = true
}
#IBAction func butnAct(_ sender: Any) {
bgView.backgroundColor = UIColor(white: 1, alpha: 0.9)//change view to bgView[![enter image description here][1]][1]
popView.isHidden = false
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.dismissView))
bgView.addGestureRecognizer(tap)//change view to bgView
}
#objc func dismissView() {
self.popView.isHidden = true
bgView.backgroundColor = .white//change view to bgView
}
#IBAction func sendButton(_ sender: Any) {
self.textLabel.text = inputField.text
}
Hi i'm new with Swift programming.
What im trying to do is Disable my button (signIn) in viewDidLoad and only enable when the textfields have text in them. Here's what i've achieved so far. (not much though!)
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
signIn.isEnabled = false
// Do any additional setup after loading the view, typically from a nib.
}
#IBOutlet weak var emailtxt: UITextField!
#IBOutlet weak var passwordtxt: UITextField!
#IBOutlet weak var signIn: UIButton!
I need help to create a function in signIn that keeps button disabled until text fields (emailtxt & passwordtxt) have text in them and then proceed.
Glad if anyone can sort me.
Thanks in advance!
First add these for all of your textFields in viewDidLoad():
emailtxt.addTarget(self, action: #selector(textFieldDidChange(_:)),
for: UIControlEvents.editingChanged)
passwordtxt.addTarget(self, action: #selector(textFieldDidChange(_:)),
for: UIControlEvents.editingChanged)
Then use this:
#objc func textFieldDidChange(_ textField: UITextField) {
self.buttonIsEnabled()
}
func buttonIsEnabled() {
var buttonIsEnabled = true
defer {
self.signIn.isEnabled = buttonIsEnabled
}
guard let emailtxt = self.emailtxt.text, !emailtxt.isEmpty else {
addButtonIsEnabled = false
return
}
guard let passwordtxt = self. passwordtxt.text, ! passwordtxt.isEmpty else {
addButtonIsEnabled = false
return
}
}
I use this way in my codes and it works well.
Even you can add more methods for additional checking to buttonIsEnabled, like:
self.checkEmailIsValid(for: emailtxt)
Of course you should handle this method before:
func checkEmailIsValid(for: String) {
//...
}
Set ViewController as delegate for emailtxt and passwordtxt like this,
override func viewDidLoad() {
super.viewDidLoad()
signIn.isEnabled = false
emailtxt.delegate = self
passwordtxt.delegate = self
// Do any additional setup after loading the view, typically from a nib.
}
Conform your ViewController to UITextFieldDelegate and enable/disable as the text input is finished,
extension ViewController: UITextFieldDelegate {
func textFieldDidEndEditing(_ textField: UITextField) {
if emailtxt.text?.isEmpty == false && passwordtxt.text?.isEmpty == false {
signIn.isEnabled = true
} else {
signIn.isEnabled = false
}
}
}
Here is the fix for your code you shared.
import UIKit
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
#objc func dismissKeyboard() {
view.endEditing(true)
}
}
extension SignInVC: UITextFieldDelegate {
func textFieldDidEndEditing(_ textField: UITextField) {
if emailtxt.text?.isEmpty == false && passwordtxt.text?.isEmpty == false {
signIn.isEnabled = true
} else {
signIn.isEnabled = false
}
}
}
class SignInVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
signIn.isEnabled = false
emailtxt.delegate = self
passwordtxt.delegate = self
self.hideKeyboardWhenTappedAround()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBOutlet weak var emailtxt: UITextField!
#IBOutlet weak var passwordtxt: UITextField!
#IBOutlet weak var signIn: UIButton!
}
What I would do is create an IBAction from one of your text fields, and set the event to Editing Changed:
The code should look like this:
#IBAction func textFieldEditingDidChange(_ sender: UITextField) {
}
You can then connect that same outlet to both of your text fields by dragging from the outlet to your remaining field. If you've connected both correctly, clicking on the circle to the left of your IBAction should show two text fields:
The action will now be fired every time text changes in either of your fields.
Then, at the top of the file, I'd create a computed property that returns false unless there is something in both fields:
var shouldEnableButton: Bool {
guard let text1 = textField1.text, let text2 = textField2.text else {
return false
}
return text1.isEmpty && text2.isEmpty ? false : true
}
Finally, we add shouldEnableButton to our IBAction:
#IBAction func textFieldEditingDidChange(_ sender: UITextField) {
button.isEnabled = shouldEnableButton
}
Important
When you connect your second text field to the outlet, it will incorrectly assign Editing Did End as its event:
Delete this event and click and drag from Editing Changed to your IBAction:
Use SwiftValidator
https://github.com/SwiftValidatorCommunity/SwiftValidator
by this, you will set validation of email & password like below
import SwiftValidator
let validator = Validator()
validator.registerField(emailTextField, errorLabel: emailErrorLabel, rules: [RequiredRule(), EmailRule(message: "Invalid email")])
// MARK: - ValidationDelegate
extension ViewController: ValidationDelegate {
func validationSuccessful() {
self.loginUser()
}
func validationFailed(_ errors:[(Validatable ,ValidationError)]) {
for (field, error) in errors {
//Handle as per need - show extra label - shake view etc
/*
if let field = field as? UITextField {
Utilities.shakeTheView(shakeView: field)
}
error.errorLabel?.text = error.errorMessage
error.errorLabel?.isHidden = false
*/
}
}
}
I'm trying to put a UITapGestureRecognizer on UIImageView in UICollectionViewCell but it does not work. Here is the code:
class ImageGalleryCell : UICollectionViewCell {
#IBOutlet weak var imgPhoto: UIImageView!
let uniqueTag = String.random()
func setup(){
let rec = UITapGestureRecognizer.init(target: self, action: #selector(tap))
imgPhoto.addGestureRecognizer(rec)
}
#objc func tap(){
print("tap")
}
}
What am I doing wrong?
Based on the code snippet you provide, it seems that you should manually set the image view isUserInteractionEnabled property to true -since it is false by default-, in setup:
func setup(){
let rec = UITapGestureRecognizer.init(target: self, action: #selector(tap))
// here we go:
imgPhoto.isUserInteractionEnabled = true
imgPhoto.addGestureRecognizer(rec)
}
Also, I would suggest to call setup method in the cell class itself, in the awakeFromNib() instead of calling it in the view controller - tableView(_:cellForRowAt:) (or any method in the view controller layer), simply like this:
class ImageGalleryCell : UICollectionViewCell {
#IBOutlet weak var imgPhoto: UIImageView!
//let uniqueTag = String.random()
func setup(){
let rec = UITapGestureRecognizer.init(target: self, action: #selector(tap))
// here we go:
imgPhoto.isUserInteractionEnabled = true
imgPhoto.addGestureRecognizer(rec)
}
#objc func tap(){
print("tap")
}
override func awakeFromNib() {
super.awakeFromNib()
setup()
}
}
That's because such a behavior should be setup only for one time when delivering the cell but not each time it has been shown.
I'm very new to all of this and I found some code that got me understanding some of this syntax. I'm trying to create a textfield that lets me type in a value that updates the stepper's value. The stepper currently works (updates the uitextfield) but when I change the value in the textfield it doesn't update the stepper's value, so when I click on the stepper, it reverts back to whatever value it was before I typed in a value... Can anyone tell me why the two functions STracksValueDidChange and CTrackValueDidChange have errors?
Here's my code so far:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var STracks: UITextField!
#IBOutlet weak var STracksStepper: UIStepper!
#IBOutlet weak var CTracks: UITextField!
#IBOutlet weak var CTrackStepper: UIStepper!
override func viewDidLoad() {
super.viewDidLoad()
STracksStepper.autorepeat = true
STracksStepper.maximumValue = 100.0
STracksStepper.minimumValue = 2.0
STracksStepper.stepValue = 2.0
print(STracksStepper.value)
STracks.text = "\(Int(STracksStepper.value))"
STracksStepper.addTarget(self, action: "SstepperValueDidChange:", forControlEvents: .ValueChanged)
STracks.addTarget(self, action: "STextValueDidChange:", forControlEvents: .ValueChanged)
CTrackStepper.autorepeat = true
CTrackStepper.maximumValue = 100.0
CTrackStepper.minimumValue = 2.0
CTrackStepper.stepValue = 2.0
print(CTrackStepper.value)
CTracks.text = "\(Int(CTrackStepper.value))"
CTrackStepper.addTarget(self, action: "CstepperValueDidChange:", forControlEvents: .ValueChanged)
CTracks.addTarget(self, action: "CTextValueDidChange:", forControlEvents: .ValueChanged)
}
//Steppers will update UITextFields
func SstepperValueDidChange(stepper: UIStepper) {
let stepperMapping: [UIStepper: UITextField] = [STracksStepper: STracks]
stepperMapping[stepper]!.text = "\(Int(stepper.value))"
}
func STracksValueDidChange(SText: UITextField) {
let STextMapping: [UITextField: UIStepper] = [STracks: STracksStepper]
STextMapping[SText]!.value = "(SText.text)"
}
func CstepperValueDidChange(stepper: UIStepper) {
let stepperMapping: [UIStepper: UITextField] = [CTrackStepper: CTracks]
stepperMapping[stepper]!.text = "\(Int(stepper.value))"
}
func CTrackValueDidChange(CText: UITextField) {
let CTextMapping: [UITextField: UIStepper] = [CTracks: CTrackStepper]
CTextMapping[CText]!.value = "(CText.text)"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Try something like this.
CTrackStepper.value = Double(Textfield.text)
I am not so sure what the mapping is in your code.
But i don't think you need it for changing the value.
Update, made a project my self:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var textfield: UITextField!
#IBOutlet weak var stepper: UIStepper!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func stepperValueChanged(sender: UIStepper) {
textfield.text = String(sender.value)
}
#IBAction func valueChanged(sender: UITextField) {
if Double(sender.text!) != nil {
stepper.value = Double(sender.text!)!
}
}
}
For steppervaluechanged and valuechanged just drag from uistepper and textfield and choose action and change the Anyobject to Uistepper of Uitextfield.
Good luck :)
My working code slides a label of to the left of the window and slide it again to the right to show it fully. Since I am learning swift, I would appreciate your comments to improve on it. I included some factoring which failed to work.
Working code:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var myLabel: UILabel!
#IBOutlet var parentView: UIView!
var swipGestureRight: UISwipeGestureRecognizer!
var swipGestureLeft: UISwipeGestureRecognizer!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
myLabel.userInteractionEnabled = true
swipGestureLeft = UISwipeGestureRecognizer(target: self, action: Selector("swipViewLeft:"))
swipGestureLeft.direction = .Left
parentView.gestureRecognizers = [swipGestureLeft]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func swipViewLeft(gesture: UISwipeGestureRecognizer){
UIView.animateWithDuration(0.3, animations: {self.myLabel.center.x -= (self.myLabel.bounds.width)}, completion: nil)
swipGestureRight = UISwipeGestureRecognizer(target: self, action: Selector("swipViewRight:"))
swipGestureRight.direction = .Right
parentView.gestureRecognizers = [swipGestureRight]
}
func swipViewRight(gesture: UISwipeGestureRecognizer){
UIView.animateWithDuration(0.3, animations: {self.myLabel.center.x += (self.myLabel.bounds.width)}, completion: nil)
swipGestureLeft = UISwipeGestureRecognizer(target: self, action: Selector("swipViewLeft:"))
swipGestureLeft.direction = .Left
parentView.gestureRecognizers = [swipGestureLeft]
}
}
Failed after trying to improve:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var myLabel: UILabel!
#IBOutlet var parentView: UIView!
var swipGestureRecognizer: UISwipeGestureRecognizer!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
myLabel.userInteractionEnabled = true
swipGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipping:"))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func swipping(gestureRecognizer: UISwipeGestureRecognizer){
if gestureRecognizer.direction == .Left {
parentView.gestureRecognizers = [gestureRecognizer]
UIView.animateWithDuration(0.3, animations: {self.myLabel.center.x -= (self.myLabel.bounds.width)}, completion: nil)
} else {
UIView.animateWithDuration(0.3, animations: {self.myLabel.center.x += (self.myLabel.bounds.width)}, completion: nil)
}
}
}
How about move
parentView.gestureRecognizers = [gestureRecognizer]
to viewDidLoad().