Added a pickerView using a UITextField.
I am trying to trigger the pickerView programmatically.
func myTypeDetector (textfield: UITextField) {
typeLabel.delegate = self
self.setType(textField: typeLabel)
}
override func viewDidLoad() {
super.viewDidLoad()
typeLabel.addTarget(self, action: #selector(NewSpeciesVC.myTypeDetector(textfield:)), for: UIControlEvents.touchUpInside)
}
I have found how to do this via UIButton
self.typeLabel.sendActions(for: .touchUpInside)
However, this is not working for the UITextField.
I would like to programmatically create the event with the TextField, or create the PickerView event using a UIButton.
Any suggestions?
I needed to do the same thing and figured it out:
self.typelabel.becomeFirstResponder()
Related
I have the below script, and when the keyboard is up, if I click around it in this UIController, it will hide the keyboard, but it will also click on the cell. I do not want that to happen.
I know that cancelsTouchesInView will control that feature... and if I set it to true, it wont click on the cell, BUT I can't click on the cell after the keyboard is hidden.
Is there a good solution for this?
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard2))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
#objc func dismissKeyboard2() {
view.endEditing(true)
}
}
Your best bet may be to disable user interaction on your tableView when the keyboard is shown, and then re-enable it when you end editing.
However, you might give this a try and see if it does what you need.
Change your signature for the dismissKeyboard2 func to get a reference to the tap gesture recognizer and remove it when you end editing:
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard2(_:)))
tapGesture.cancelsTouchesInView = true
view.addGestureRecognizer(tapGesture)
}
#objc func dismissKeyboard2(_ g: UITapGestureRecognizer) {
view.endEditing(true)
view.removeGestureRecognizer(g)
}
}
I generated a multiple UIButton and UIView using loop, the problem is, I want that generated UIView to be hidden when the generated UIButton was clicked,
The question is, how can I pass the UIView on a UIButton click event so that the system knows what UIView will going to be hidden
This is my code that generate UIButtons and UIViews
for (key, value) in myStringsArray {
let myButton = UIButton()
let myView = UIView()
panelButton.tag = value
panelButton.addTarget(self, action: #selector(onMyButtonClick), for: .touchUpInside)
}
The only data that I can pass on .tag was Int
And this is my onMyButtonClick function that listen on click event of the UIButton
#objc func onMyButtonClick (sender: UIButton) {
print(sender.tag)
}
What I want to do is to have a click listener function that is working like this
func clickMe (view: UIView, isOpen: Bool) {
view.isHidden = isOpen
}
You can assign the button and the view the same tag.
then you can find the view by tag and hide it.
#objc func onMyButtonClick (sender: UIButton) {
print(sender.tag)
if let foundView = view.viewWithTag(sender.tag) {
foundView.isHidden = true
}
}
I have three UITextfields (customerRequestTextField , fundsAvailableTextField and amountOwedMarketTextField) on the same ViewController.
I would like to set up the amountOwedTextField so that it automatically updates its field by subtracting the data entered into the other two textfields (customerRequestTextField - fundsAvailableTextField).
Should I use NSNotifications? Or a selector? Or something else?
I have tried setting up a selector, but my version does not work.
This is declared globally.
var amountOwedToMarket
This code is located in the viewDidLoad().
amountOwedMarketTextField.text = String(amountOwedToMarket)
amountOwedMarketTextField.addTarget(self, action: #selector(self.textFieldDidChange(sender:)), for: .editingChanged)
}
This is declared outside of the viewDidLoad()
#objc func textFieldDidChange(sender: UITextField) -> Int {
return Int(amountCustomerRequestTextField.text!) ?? 21
}
You need to add the target to textfields that you want to listen to their changes
// in viewDidLoad
amountCustomerRequestTextField.addTarget(self, action: #selector(self.textFieldDidChange), for: .editingChanged)
fundsAvailableTextField.addTarget(self, action: #selector(self.textFieldDidChange), for: .editingChanged)
#objc func textFieldDidChange(_ sender: UITextField) {
if let first = Int(amountCustomerRequestTextField.text!) , let second = Int(fundsAvailableTextField.text!) {
amountOwedMarketTextField.text = "\(first - second)"
}
}
I feel as though I'm not understanding the Swift #selectors properly. I'm trying to connect a button to a method from another class.
I have a class to print the button when pushed:
class printThings {
#IBAction func printMe(_ sender: UIButton){
print("Button Pushed.")
}
}
And then the ViewController:
class ViewController : UIViewController {
override func ViewDidLoad(){
super.viewDidLoad()
//button setup here
let printMe = printThings()
button.addTarget(printMe, action: #selector(printMe.printMe(_:)), for: .touchUpInside)
//add button to subview
}
}
This never triggers the print statement in the class. I'm sure I'm missing something simple.
Thanks.
The problem is that printMe is a temporary, local variable:
let printMe = printThings() // local variable
button.addTarget(printMe, action: #selector(printMe.printMe(_:)), for: .touchUpInside)
// ... and then viewDidLoad ends, and `printMe` vanishes
So by the time you push the button, printMe has vanished; there is no one to send the button message to.
If you want this to work, you need to make printMe persist:
class ViewController : UIViewController {
let printMe = printThings() // now it's a _property_ and will persist
override func viewDidLoad(){
super.viewDidLoad()
//button setup here
button.addTarget(self.printMe, action: #selector(self.printMe.printMe(_:)), for: .touchUpInside)
//add button to subview
}
}
I have a textfield and textview in my custom tableviewcell.
I have 4 different prototype cell with 4 different class created. there is a textfield in 1 prototype cell and a textview in the other.
I am not sure how I can do it and I dont understand the obj-c answers out there.
I've tried
override func viewDidLoad() {
super.viewDidLoad()
let tap: UIGestureRecognizer = UIGestureRecognizer(target: self, action: #selector(UIInputViewController.dismissKeyboard))
view.addGestureRecognizer(tap) }
and
func dismissKeyboard() {
self.view.endEditing(true)
}
I wanted to try
UITextFieldDelegate and touchesbegan and textfieldshouldreturn method, but there are no textfields to call in my tableviewcontroller.
Go to attributes inspector in storyboard and click tableView and set keyboard to dismiss interactively.
Or
Set textField delegate and implement
override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self
}
func textFieldShouldReturn(textField: UITextField) -> Bool // called when 'return' key pressed. return false to ignore.
{
textField.resignFirstResponder()
return true
}
I had same problem, you can solve this with IQKeyboardManager, you don't need to complicate things with the custom cell classes, just install it on your project, when you're all set with installation, in the appDelegate inside the didFinishLaunchingWithOptions method, add this line of code:
IQKeyboardManager.shared.shouldResignOnTouchOutside = true
Get IQKeyboardManager from here.