How can I reach UITextfield in subviews in Swift? - ios

Here is the objects hierarchy in my ViewController in Main.Storyboard
Button same size with superview. I would like to close the keyboard with the button click action that's why I wrote this code scope:
#IBAction func btnCloseKeyboardClick(_ sender: UIButton) {
print("A")
for viewItem in self.view.subviews
{
print("B")
if (viewItem is UITextField)
{
print("C")
let tField = viewItem as! UITextField
tField.resignFirstResponder()
}
}
}
but it does not close the keyboard means I don't see print("C")
I solved this problem like in this link but I would like to ask above situation also

If you only have one textfield in your view and want to dismiss keyboard for that, the simple way is to call view.endEditing(true) in your button action

Related

How to prevent dismissing keyboard when the user did tap on the button

I have two UI Components in my UIViewController. a UITextField and UIButton.
When the view did load I call textField.becomeFirstResponder() to show the keyboard. the problem is I don't wanna dismiss the keyboard when the user taps on the button.
you can try to implement the UITextFieldDelegate and then prevent the textfield to lose the focus implementing the method:
var shouldEndEditing = false
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
shouldEndEditing = true
}
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
return shouldEndEditing
}

Keyboard prevents data entry

I have many textFields to fill, i use below code to move the VC modally
#objc func addNewRestaurant() {
let pushController = RestaurantAddController()
pushController.modalPresentationStyle = .fullScreen //or .overFullScreen for transparency
self.present(pushController, animated: true, completion: nil)
}
And below code to move to next line when i press enter
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if let nextField = view.viewWithTag(textField.tag + 1) as? UITextField {
textField.resignFirstResponder()
nextField.becomeFirstResponder()
}
But my screen always remains covered with keyboard like below and i do not see what beneath, even if i cancel it when i reactivate the keyboard , once again fields are hidden
UPDATE SOLUTION -
I ended up using IQKeyboardManager, its giving 3 warnings but not able to understand how it was done, not a solution from apple but need to move on with project, this is what i am getting now b, see below, thanks
Put your all the fields under scroll view and use "TPKeyboardAvoidingScrollView" class for better experience
Here is an answer. Here is an answer Https://stackoverflow.com/q/26070242/13975969

Hiding UIButton when pushed, then showing it when another button is pushed

I have three buttons that change the colour of my background. I want to hide the RED button when it's pushed. The show it if GREEN or BLUE button is pushed.
The same goes for the GREEN and BLUE buttons.
I can't find a way to call removeFromSuperview. In ObjectiveC I used to do mybutton.hidden = true
but this doesn't work.
ViewController: UIViewController {
#IBAction func RED(_ sender: Any) {
print("background was \(String(describing: self.view.backgroundColor))")
self.view.backgroundColor = UIColor.red
print("background is now \(String(describing: self.view.backgroundColor))")
}
#IBAction func GREEN(_ sender: Any) {
print("background was \(String(describing: self.view.backgroundColor))")
self.view.backgroundColor = UIColor.green
print("background is now \(String(describing: self.view.backgroundColor))")
}
#IBAction func BLUE(_ sender: UIButton) {
print("background was \(String(describing: self.view.backgroundColor))")
self.view.backgroundColor = UIColor.blue
print("background is now \(String(describing: self.view.backgroundColor))")
}
You need to create an IBOutlet for each button in order to access the "isHidden" property. Right now you only have an IBAction defined for each (at least in the code you've provided). To create the IBOutlet, control-drag from the button to your view controller's code, similar to what you did to create the IBActions you already have. In the popup, make sure that "Connection" says "Outlet" and "Type" is "UIButton". Name them as desired (e.g., redButton, blueButton). Then you can type "redButton.isHidden = true" in the appropriate location.
An IBAction only allows you to control what the button will do when it is pressed. An IBOutlet is required to access the properties of a UIButton.

When pass data between 2 ViewController form valide in Swift 3.1

I want to pass data between two navigation controllers. I can this with perform segue method. But I have a problem. My first navigation control has 3 textfields and a button. When click the button push other navigationcontroller. How can I perfom push to other navigationcontrol just when textfields filled. Now I click the button even if textfields empty push other nc. Tahnk you.
You need to implement shouldPerformSegue(withIdentifier:sender:) and return true only when it is allowed to perform the segue, otherwise return false.
shouldPerformSegue() is called immediately prior the segue is performed.
Add outlets for the textfields in form, say outlets are named as textfield1, textfield2, textfield3
Implement the following delegate method
func textFieldDidEndEditing(_ textField: UITextField) {
if (textField == textfield3) {
if self.allTextFieldsAreFilled() { //Check if all textfields are filled
//Add your push code here
self.navigationController?.pushViewController(viewController, animated: true)
}
}
}
Add this validating function
func allTextFieldsAreFilled() -> Bool {
if (textfield1.text?.characters.count > 0 && textfield2.text?.characters.count > 0 && textfield3.text?.characters.count > 0) {
return true
}
return false
}
Be sure to set delegate to all the textfields

Click rootViewController through viewController

I'm trying to add TopView in my application, it will be the same for each views. I do it like this
let vcTopMenu = storyboard?.instantiateViewControllerWithIdentifier("TopMenu")
let win:UIWindow = UIApplication.sharedApplication().delegate!.window!!
win.rootViewController = vcTopMenu
win.makeKeyAndVisible()
But when I add other viewControllers (I do it transparent) I can see buttons of TopView, but I can't click on it. It's a code from TopView
override func viewDidLoad()
{
super.viewDidLoad()
print("loaded")
}
#IBAction func btn(sender: AnyObject)
{
print("do something")
}
I see "loaded", but clicking doesn't work, how can I click through view? Thanks!
If I understand your question correctly, you're placing a translucent/transparent UIView on top of another UIView with a button you want to press?
The topmost UIView by default receives the touches. More on this here.
It's not a very standard/practical way to do things, but if you absolutely must, check out this answer: https://stackoverflow.com/a/4010809/4396258

Resources