I am trying to have two non-editable UITextField display name and age. I have an Edit UIBarButtonItem in my Navigation Bar that I want to be able to trigger the UITextField to be editable when that button is pressed.
In my Interface Builder, I have the User Interaction Enabled option unchecked for the two UITextFields. Do I need to add ageText.UserInteractionEnabled = true? I'm at a loss here.
class UserProfileVC: UIViewController,
UITextFieldDelegate, UINavigationControllerDelegate {
#IBOutlet weak var infoBorder: UILabel!
#IBOutlet weak var nameText: UITextField!
#IBOutlet weak var ageText: UITextField!
var textFields:[UITextField] = []
#IBAction func editButton(sender: UIBarButtonItem) {
nameText.becomeFirstResponder()
ageText.becomeFirstResponder()
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
var currentTextField = textFields[0]
if (currentTextField == textField) {
currentTextField = textFields[1]
currentTextField.becomeFirstResponder()
} else {
currentTextField.resignFirstResponder()
}
return true
}
}
Yes, you want to do
nameText.userInteractionEnabled = true
ageText.userInteractionEnabled = true
and possibly
nameText.becomeFirstResponder()
when the edit button gets pressed the first time. You will probably also want to change the "Edit" button do a "Done" button. When the user presses the done button, you'll want to make sure you disable user interaction and resign first responder for both text fields.
You should have a Bool Variable. Set it to 'false' then when the button is pressed you toggle it to 'true'. Depending on its state just run to different methods which essentially allows you to edit or not.
Something like this:
var editTextFieldToggle: Bool = false
#IBoutlet var textFieldToggle: UIButton!
#IBAction func textFieldToggle_Action(sender: UIButton){
editTextFieldToggle = !editTextFieldToggle //switches button ON/OFF
if editTextFieldToggle == true {
textFieldActive()
} else {
textFieldDeactive()
}
}
textFieldActive(){
//Turn things ON
nameText.enabled == true
ageText.enabled == true
}
textFieldDeactive(){ //Add anything else
//Turn things OFF
nameText.enabled == false
ageText.enabled == false
}
Related
How can I make an if statement in swift that when I click the search button as shown in the picture below to show the search bar from isHidden to isSelected
Basically when I press the searchImage the searchBar will show from its initial isHidden to isSelected
pic
#IBAction func searchButton(_ sender: Any) {
}
#IBOutlet weak var searchField: UISearchBar!
I don't know what you need exactly but if you need to switch isHidden or isSelected you can add var to keep the search bar state like:
var isSearchFieldHidden: Bool = false{
didSet{
self.searchField.isHidden = isSearchFieldHidden
}
}
#IBAction func searchButton(_ sender: Any) {
isSearchFieldHidden.toggle()
}
#IBOutlet weak var searchField: UISearchBar!
Try to use
Button(action: {
isHidden.toggle()
}) {
Image("search_icon")
}
!isHidden{
// any view: Button, TextField etc.
your_searchBarView()
}
and read: https://developer.apple.com/documentation/swiftui/text/hidden()#declaration
I'm new to swift programming and I'm trying to disable the save button until all other required buttons are selected, but when I try to do that with the button.isEnabled = false, it doesn't change even when all the buttons are selected. here is a sample code:
func disableButton () {
if firstButton.isSelected && rightButton.isSelected {
saveButton.isEnabled = true
} else {
saveButton.isEnabled = false
}
}
when I remove the last line the save button works but when I put it back it's disabled even when the two other buttons are selected.
Assuming you have used Storyboard you have to link the 2 buttons into #IBActions , and within those #IBAction methods manipulate the isSelected property. Refer the example below.
NOTE - Read the comments
class ViewController: UIViewController {
// 2 buttons that we will set the isSelected property
#IBOutlet weak var button1: UIButton!
#IBOutlet weak var button2: UIButton!
// Button to disable/enable
#IBOutlet weak var finalButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
finalButton.isEnabled = false // setting the button as disabled
}
/// This is the function triggered when you click on the "button1"
#IBAction func didPressButton1(_ sender: UIButton) {
// Here we will set the isSelected property of "sender" parameter, which is the button that calls this function. That is button 1
sender.isSelected = sender.isSelected ? false : true
//calling this function to make any updates to the UI if needed
disableButton()
}
/// This is the function triggered when you click on the "button2"
#IBAction func didPressButton2(_ sender: UIButton) {
// Here we will set the isSelected property of "sender" parameter, which is the button that calls this function. That is button2
sender.isSelected = sender.isSelected ? false : true
//calling this function to make any updates to the UI if needed
disableButton()
}
func disableButton () {
if button1.isSelected && button2.isSelected {
finalButton.isEnabled = true
} else {
finalButton.isEnabled = false
}
}
}
What happens here is you will set the isSelected property of the button that calls the function in the function itself, and run the disableButton() function every time it is invoked for UI updates.
The final result will be,
I have password UITextfield that is currently turned on as secure entry. I would like to show the user the password he has typed inform of text again in the UITextfield when the UISwitch is turned on. Here is my implementation that so far. It works when i print it out in the console but doesn't in the UITextfield. I would like to show it on once the UISwitch is turned on and off when the UISwitch is turned off.
#IBOutlet weak var existingPasswordTexfField: UITextField!
#IBOutlet weak var newPasswordTextField: UITextField!
#IBOutlet weak var changePasswordSwitch: UISwitch!
#IBAction func showPassword(_ sender: UISwitch) {
if changePasswordSwitch.isOn {
guard let oldText = existingPasswordTexfField.text else { return }
if existingPasswordTexfField.isSecureTextEntry {
existingPasswordTexfField.text = oldText
} else {
print("Pawword is already secure")
}
}
}
According to Apple Doc ìsSecureTextEntry is a writable property
So you in your showPassword IBAction you need to set it toggle it at some point :
existingPasswordTexfField.isSecureTextEntry = false
or just
existingPasswordTexfField.isSecureTextEntry = !changePasswordSwitch.isOn
You'll need to change the isSecureTextEntry flag on the textfield when the user flips the switch.
#IBAction func showPassword(_ sender: UISwitch) {
existingPasswordTextField.isSecureTextEntry = changePasswordSwitch.isOn
}
This is also works
existingPasswordTexfField.isSecureTextEntry = changePasswordSwitch.isOn ? false : true
I am not sure why textFieldShouldBeginEditing returns all of the UiTextField
File: PaymentViewControllerDummy.swift
class PaymentViewControllerDummy: UIViewController, UITextFieldDelegate {
#IBOutlet weak var dobTextField: UITextField!
#IBOutlet weak var nameTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
dobTextField.tag = 1
nameTextField.tag = 2
dobTextField.delegate = self
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
println("Tage From textFile: \(textField.tag) ")
println("Tage From dobTextField: \(dobTextField.tag) ")
if(textField.tag == dobTextField.tag) {
println("You are editing date of birth")
return false
} else {
return true
}
}
}
All the IBOutlets are connected. This is very standard code. I have done this times after time however whichever textfield I press textField == self.dobTextField comes back true
Console result:
Am I missing something?
Edit
Here is the Interface builder screens
NOTE
I made a standalone project and copy the codes to the project and it worked as it should however it is not working in this project. Could it be something in the StoryBoard ?
You shouldn't check UI objects with equality comparison.
You had better use tags (or maybe labels, not preferred however) in order to conduct protocol calls over different UI objects.
class PaymentViewControllerDummy: UIViewController, UITextFieldDelegate {
#IBOutlet weak var dobTextField: UITextField!
#IBOutlet weak var nameTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
dobTextField.delegate = self
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
println(textField.tag)
println(dobTextField.tag)
if(textField.tag == dobTextField.tag) {
println("You are editing date of birth")
return false
} else {
return true
}
}
}
Also you DON'T have to refer self if you want to access instance variables, if you are not in a block (closure).
The best thing to access text fields in text field did editing method is via tags. Set the tag of both the text fields let say self.dobTextField.tag=1 & self.nameTextField.tag=2. Then in
func textFieldShouldBeginEditing(textField: UITextField) -> Bool
println(textField.tag)
println(dobTextField.tag)
if(textField.tag == 1) //dob text field is currently in edit mode
{
println("You are editing date of birth")
return true
}
else // name text field is in edit mode
{
return true
}
}
I've implemented an edit burron for my custom tableviewcontroller without using the default edit button. I have put a UIBarButton item in my storyboard and linked with IBOutlet in my custom class. I've implemented all these simple mechanisms for changing the button title:
class DetailTableViewController: UITableViewController {
var selectedMedicine: NSManagedObject?
var edit: Bool = false
#IBOutlet var editButton: UIBarButtonItem
#IBOutlet var nameTextField: UITextField
#IBOutlet var noteTextView: UITextView
#IBAction func enableEditing(sender: AnyObject) {
if !edit
{
println("editing")
self.edit = true
self.navigationItem.rightBarButtonItem.title = "Done"
}
else
{
self.edit = false
self.navigationItem.rightBarButtonItem.title = "Edit"
}
}
The title changes but there's something strange in the transition from Edit to Done because it's a little jerky.
I've found another one with my same problem but no one answered him.
You can look at this video for undertsand the bad animation i mean video
Instead of manually setting the bar button title and tracking state, you can use the built-in UIViewController.editButtonItem and UIViewController.editing property:
func viewWillAppear(animated:Bool) {
self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
#IBAction func enableEditing(sender: AnyObject) {
self.editing = !self.editing
}