Press search button to show the searchBar from isHidden to isSelected - ios

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

Related

UIButton is disabled - Swift

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,

How to unhide the button after all switches in ON condition?

I have 2 UISwitch and a Button, in viewDidLoad, I set the button to be hidden and disabled, I want only my button to be not hidden if those 2 switch is in ON state, otherwise, I want my button to hide again. is there any method from UI Switch delegate that can be used ? how do I do that in Swift ?
here is the code I use
import UIKit
class AskingAuthorizationVC: UIViewController {
#IBOutlet weak var locationSwitch: DesignableSwitch!
#IBOutlet weak var notificationSwitch: DesignableSwitch!
#IBOutlet weak var nextButton: DesignableButton!
override func viewDidLoad() {
super.viewDidLoad()
// initial state
nextButton.isHidden = true
nextButton.isEnabled = false
notificationSwitch.isOn = false
locationSwitch.isOn = false
}
#IBAction func signUpButtonDidPressed(_ sender: Any) {
performSegue(withIdentifier: "toAuthenticationVC", sender: nil)
}
}
Hook both of the UISwitch-s as IBActions & IBOutlets
#IBAction func oweSwitch(_ sender: UISwitch) {
self.mybutton.isHidden = !(switch1.isOn && switch2.isOn)
}

How to hide button and disable button

I'm trying to create small restaurant app for employees. There I have table numbers as a button if the user clicks that button I want that clicked button to be disabled and I want textfield and another ok button to appear. And if I click on disable button I want that to be enabled.
import UIKit
class ViewController: UIViewController {
var total = 0
#IBOutlet weak var okButton: UIButton!
#IBOutlet weak var userInput: UILabel!
#IBOutlet weak var userValue: UITextField!
#IBAction func okButton(_ sender: UIButton) {
if userValue.text != nil{
userInput.text = String(0)
let userValueint: Int? = Int(userValue.text!)
total = total + userValueint!
let convertText = String(total)
userInput.text = convertText
userValue.text = String(0)
userValue.isHidden = true
okButton!.isHidden = true
} else {
print("Please Inter values")
}
}
#IBAction func buttenPressed(_ sender: UIButton) {
userValue.isHidden = false
okButton.isEnabled = true
}
override func viewDidLoad() {
super.viewDidLoad()
userValue.isHidden = true
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
okButton.isHidden = false
}
}
So far I'm able to hide textField at the beginning and able to enabled when table button is clicked, but I can't hide ok button and disable the table button. Any suggestion?
First of all, you can't clicked a disabled button. Second, use viewDidLoad instead of viewWillAppear.
I guess your table button is sender so disable sender and show ok button
#IBAction func buttenPressed(_ sender: UIButton) {
sender.isEnabled = false
userValue.isHidden = false
okButton.isHidden = false
okButton.isEnabled = true
}

How to change label to original text after it's been changed with a function in Swift?

I have a button that changes a text view when it's pressed, I want it to do the changed text to be switched into the original text when the button is pressed again.
My Code:
//An IBOutlet for the textview
#IBOutlet weak var changingText: UITextView!
//The button that changed the text
#IBAction func viewChangedText(sender: AnyObject) {
changingText.text = "Changed Text"
}
Now I want the text changed back when the button is pressed again.
Note: The original text is in in the storyboard, and if possible. could you help me change the button's text to "View" when the original text is there and change the button's text to "Hide" when the changed text is there.
var didChange = false
#IBOutlet weak var changingText: UITextView!
//The button that changed the text
#IBAction func viewChangedText(sender: AnyObject) {
didChange = !didChange
if didChange {
changingText.text = "Changed Text"
}else{
changingText.text = "Original text"
}
}
#nathanwhy has a nice idea with the switch-bool. Building upon that idea you could have something like this:
#IBOutlet weak var changingTextView: UITextView!
var showOriginalText : Bool {
didSet {
changingTextView.text = showOriginalText ? "original text" : "other text"
}
}
override func viewDidLoad() {
super.viewDidLoad()
// or wherever else it makes sense
showOriginalText = true
}
#IBAction func changeButtonTapped(sender: UIButton) {
showOriginalText = !showOriginalText
}

Changing UITextfield from Non-Editable to Editable - Swift

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
}

Resources