I have two radio buttons, each with it's own IBAction. The first performs flawlessly. The second has an if-else condition and doesn't respond to the first tap but then responds to each tap thereafter. From looking at similar questions, I have sense that the first tap is just evaluating my function before performing the action but I am unable to figure out a way to get the button to respond on the first tap.
#IBAction func radioSchedule(_ sender: Any) {
print(timePicker.isHidden)
if timePicker.isHidden == true {
timePicker.isHidden = false
hideButton.isHidden = true
timeView.isHidden = false
itemTypeField.text = "Schedule"
saveButton.isHidden = false
} else {
timePicker.isHidden = true
timeView.isHidden = true
}
}
My guess is your timePicker sits inside your timeView and timeView is initially hidden, but your timePicker is not, and that is what is causing your issue.
If so, you could probably fix this by just judging your if/else off of your timeView, and then you wouldn't even have to worry about setting the timePicker to hidden or not.
i.e.:
#IBAction func radioSchedule(_ sender: Any) {
print(timeView.isHidden)
if timeView.isHidden {
hideButton.isHidden = true
timeView.isHidden = false
itemTypeField.text = "Schedule"
saveButton.isHidden = false
} else {
timeView.isHidden = true
}
}
Looks like setting timepicker.ishidden is creating the problem modify your code something like this:-
#IBAction func radioSchedule(_ sender: Any) {
print(timePicker.isHidden)
//Here updating the isHidden property first. If its false then initially will set to true.
timePicker.isHidden = !timePicker.isHidden
if timePicker.isHidden == true {
hideButton.isHidden = true
timeView.isHidden = false
itemTypeField.text = "Schedule"
saveButton.isHidden = false
} else {
timeView.isHidden = true
}
}
Related
I am not sure about how to do this. I am trying to allow it so that when the function "end" happens, the score can no longer increase when button is tapped.
#IBAction func Button(_ sender: UIButton) {
score += 1
}
func end() {
score += 0
}
You can disable the button
func end() {
yourButton.isEnabled = false
}
There are two ways:
yourButton_Outlet_Name.isEnabled = false
yourButton_Outlet_Name.isUserInteractionEnabled = false
Make sure to use it on the button outlet, not the UIButton class
i have a button that when selected, a white box appears over the title text of the button. I dont want this white box to appear. See images.
button state unselected
button state selected
If i remove the if statement and have the button so it can only be selected but not deselected, on selection, this white box doesn't appear. The code controlling this is below...
#IBAction func backButtonPressed(_ sender: Any) {
if let button = sender as? UIButton {
if button.isSelected {
createWorkoutButton.isEnabled = false
backButton.backgroundColor = #colorLiteral
backButtonPressed = false
backButton.isSelected = false
} else {
createWorkoutButton.isEnabled = true
backButton.backgroundColor = #colorLiteral
backButtonPressed = true
backButton.isSelected = true
}
}
}
Just found a solution. Add this line of code -
button.tintColor = .clear
Now -
#IBAction func backButtonPressed(_ sender: Any) {
if let button = sender as? UIButton {
if button.isSelected {
createWorkoutButton.isEnabled = false
backButton.backgroundColor = #colorLiteral
backButtonPressed = false
backButton.isSelected = false
button.tintColor = .clear // Add this line of code
} else {
createWorkoutButton.isEnabled = true
backButton.backgroundColor = #colorLiteral
backButtonPressed = true
backButton.isSelected = true
button.tintColor = .clear
}
}
}
In story board use button type custom.
When I press the button hide to my text, after I press button to show my text. Where is my fault? My English very bad, sorry...
#IBOutlet weak var myHiddenText: UILabel!
#IBAction func showBtn(sender: AnyObject) {
myHiddenText.hidden = true
if myHiddenText.hidden == true {
myHiddenText.hidden = false
}
if myHiddenText.hidden == false {
myHiddenText.hidden = true
}
}
If you want the button to toggle the hidden property of the label, simply do this:
#IBAction func showBtn(sender: AnyObject) {
myHiddenText.hidden = !myHiddenText.hidden
}
Your main issue was this line:
myHiddenText.hidden = true
This was hiding the label every time then your if statement would always be true and show your label. Then the second if statement would be true and hide the label again.
So another option to fix your code would be:
#IBAction func showBtn(sender: AnyObject) {
if myHiddenText.hidden == true {
myHiddenText.hidden = false
} else {
myHiddenText.hidden = true
}
}
I know this may not help you but it may help someone else. If you want to hide multiple objects you could do this.
Step 1: Reference all the objects you want to hide with #IBOutlet
Step 2: Declare this variable
var buttons_are_hidden: Bool = false
Step 3: Put this code where you need it
if self.buttons_are_hidden == true {
self.first_object.isHidden = true
self.second_object.isHidden = true
self.third_object.isHidden = true
self.buttons_are_hidden = true
} else {
self.first_object.isHidden = true
self.second_object.isHidden = true
self.third_object.isHidden = true
self.buttons_are_hidden = true
}
Hope it helps someone!
With UIKitPlus library you can do it simply like this
class MyViewController: ViewController {
#State var hiddency = false
override func buildUI() {
super.buildUI()
body {
VStack {
View().height(10).background(.red).hidden($hiddency)
View().height(10).background(.green).hidden($hiddency)
View().height(10).background(.blue).hidden($hiddency)
Button("toggle").onTapGesture {
self.hiddency.toggle()
}
}
}
}
}
Take a look at it, it works since iOS9+ and have LivePreview.
So I'm new to Swift, but trying to figure out how to disable this button when it is pressed. I have the following:
#IBAction func IBbtnUpdateTap(sender: UIButton){
if imageNumber == 0 {
IBbtnUpdateTap.enabled = false
}
I'm not sure why it's giving me problems. Any ideas?
IBbtnUpdateTap isn't the button. The button is sender
#IBAction func IBbtnUpdateTap(sender: UIButton){
if imageNumber == 0 {
sender.enabled = false
}
I would like to set the background color of a view to black when the navigation bar is hidden, and to white when the navigation bar is displayed.
The property hidesBarsOnTap is set to true in viewDidLoad. This works fine:
navigationController?.hidesBarsOnTap = true
How can I be notified when the bars are hidden and displayed?
Sorry, I made a mistake. The following code does exactly what you want. If you have a toolbar, you can set it to hide as well.
class ViewController: UIViewController {
var hidden = false {
didSet {
if let nav = navigationController {
nav.setNavigationBarHidden(hidden, animated: true)
nav.setToolbarHidden(hidden, animated: true)
view.backgroundColor = hidden ? UIColor.blackColor() : UIColor.whiteColor()
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
let recognizer = UITapGestureRecognizer(target: self, action: "tap:")
view.addGestureRecognizer(recognizer)
}
func tap(recognizer: UITapGestureRecognizer) {
if recognizer.state == .Ended {
hidden = !hidden
}
}
}
since hidesBarsOnTap is of type boolean, we can easily use it to check and use it as option like in below example:
var set : Bool = navigationController?.hidesBarsOnTap //true or false
if (set){
//do what you want when set
}else{
//do what you want when it is not set
}