How to hide a button in Xcode 6? - ios

All I want to do is keep a button hidden until another button is pressed.
For example, Button 1 is visible, but Button 2 isn't. When I press Button 1, I need Button 2 to appear.
Also, I am programming in Xcode 6 using Swift.
Thanks in advance!

The sample code for hiding a button in Swift:
import UIKit
class ViewController: UIViewController {
// Create outlet for both the button
#IBOutlet weak var button1: UIButton!
#IBOutlet weak var button2: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
//Set button2 hidden at start
button2.hidden = true
}
//Here is the action when you press button1 which is visible
#IBAction func button1(sender: AnyObject) {
//Make button2 Visible
button2.hidden = false
}
}
May be this can help you.

Related

How to display an image when button is pressed in iOS?

I am trying to display an UIImageView when a button is pressed. In my main story board, I have an ImageView(hidden) and an UIButton. How do I modify the ViewController to make that happen?
import UIKit
class ViewController: UIViewController {
#IBOutlet var ImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
ImageView.image = UIImage(named: "18.pic_hd.jpg")
}
}
This is what I currently have so far, but the program breaks and gives me Thread 1: signal SIGABRT. I am new to Xcode and Swift, so any direction is appreciated!
You should drag and drop UIButton from storyBoard to your code and use Action to have an IBAction in your viewController.
After that use imageView.isHidden = false to show the image and vice versa.
sample code:
import UIKit
class ViewController: UIViewController {
#IBOutlet var imageView: UIImageView!
#IBAction func someButton(_ sender: UIButton) {
// when button tapped this code will execute
imageView.isHidden = false
}
override func viewDidLoad() {
super.viewDidLoad()
imageView.isHidden = true
}
}
use
ImageView.isHidden = true
to hide your image, and
ImageView.isHidden = false
to show it.
add an IBAction to your button to do this.

Disable all UIButtons in UITableViewCell once a separate UIButton is pressed

Problem I've found some questions asking how to disable a particular cell button in a table view, but what I want to do is to disable all instances of a button within a table view cell when another button is pressed.
Details I have a table view which is displaying a list of exercises and number of reps in a custom cell. Within the custom cell is also a button "swap" which allows a user to swap an exercise for another one before the workout starts.
When the user hits "start workout" (which triggers a timer) I want to disable all instances of the swap button (grey them all out and make non clickable).
Code
My workout cell class is here :
class WorkoutCell : UITableViewCell {
var delegate: WorkoutCellDelegate?
#IBAction func swapButtonPressed(_ sender: Any) {
delegate?.swapButtonTapped(cell: self)
}
#IBOutlet weak var exerciseName: UILabel!
#IBOutlet weak var repsNumber: UILabel!
}
protocol WorkoutCellDelegate {
func swapButtonTapped(cell: WorkoutCell)
}
What have I tried
The way I thought to do this was to add an IBOutlet (e.g. 'swapButton') for the button and then simply do something like :
#IBAction func startButtonPressed(_ sender: UIButton) {
WorkoutCell.swapButton.isenabled = false
}
But Xcode doesn't allow you to add IBOutlets to repeating cells so I'm a bit stuck.
I'm fairly new to delegates (managed to get it working for displaying the table view) so if it has something simple to do with that sorry!
Add a property to your viewcontroller:
var swapButtonsDisabled : Bool = false
In your cellForRow do something like this:
cell.swapButton.isEnabled = !self.swapButtonsDisabled
When the start button is pressed, set swapButtonDisabled to true and reload the tableView.
1- As you connect
#IBOutlet weak var exerciseName: UILabel!
create outlet for every btn
#IBOutlet weak var btn1: UIButton!
2- Add a property to the model array in the VC to hold the state of every cell
3- When you click the main btn fire the delegate method with the btn's cell
4- In VC delegate handle method disable the btns and change the state of the array index path
5- Don't forget to check state in cellForRow
You are pretty close. First I suggest you to be more specific and have the data you need in cell and use access control:
class WorkoutCell : UITableViewCell {
var workoutSwappable: (workout: Workout, canBeSwapped: Bool)? {
didSet {
swapButton.isEnabled = workoutSwappable?.canBeSwapped == true
// TODO: do additional setup here
}
}
weak var delegate: WorkoutCellDelegate? // Needs to be weak or you will have memory leaks
#IBAction private func swapButtonPressed(_ sender: Any) {
if let workoutSwappable = workoutSwappable, workoutSwappable.canBeSwapped == true {
delegate?.workoutCell(self, didTapWorkoutSwap: workoutSwappable.workout)
}
}
#IBOutlet private var exerciseName: UILabel!
#IBOutlet private var repsNumber: UILabel!
#IBOutlet private var swapButton: UIButton!
}
Ok so now in cell for row at index path all you need is something like:
cell.workoutSwappable = (self.items[0], self.canSwap)
On delegate you now have:
func workoutCell(_ sender: WorkoutCell, didTapWorkoutSwap workout: workout) {
self.currentWorkout = workout
self.canSwap = false
self.initializeTimer()
tableView.reloadData() // This will now flush all the buttons
}

Action triggered when two buttons are tapped

How can I trigger an action in Swift when two buttons are pressed simultaneously? My code below triggers the action when one button is pressed. I would like this action to trigger only if both buttons are tapped.
class ViewController: UIViewController {
#IBOutlet weak var leftButton: UIButton!
#IBOutlet weak var rightButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
leftButton.addTarget(self, action: #selector(ViewController.leftButtonClicked), forControlEvents: .TouchUpInside)
}
func leftButtonClicked() {
// Do something
}
}
Do I need to use UIGestureRecognizer or can this be done with the addTarget action above? How would the code look like?
With the help of the comment above I have now written the solution in Swift and it turns out to be very simple:
class ViewController: UIViewController {
#IBOutlet weak var leftButton: UIButton!
#IBOutlet weak var rightButton: UIButton!
#IBAction func touchButton(sender: AnyObject) {
if leftButton.touchInside && rightButton.touchInside {
print("Two buttons pressed")
}
I removed the clutter that is not relevant for the solution.

Referencing a button in Swift

So I'm pretty new but I'm trying to reference a button from another. I disable the first button when I press it, but I want to enable it when I press the other button. Check this out. It doesn't like my second sender reference.
#IBAction func IBbtnUpdateTap(sender: UIButton){
sender.enabled = false
}
#IBAction func addSpinsButton() {
sender.enabled = true
}
Any ideas from you coding wizards?
The problem is that the context of your function addSpinsButton as no sender variable. You must add a reference to your button in the class.
You can do this with something like:
#IBOutlet weak var myButton: UIButton!
Link the button in interface builder (same way you have done with your function, but for the outlet)
Then your addSpinsButton will become:
#IBAction func addSpinsButton() {
myButton.enabled = true
}
what you need is something like this:
class ViewController: UIViewController {
#IBOutlet weak var button1: UIButton!
#IBOutlet weak var button2: UIButton!
#IBAction func button1Tapped(sender: UIButton) {
sender.enabled = false
}
#IBAction func button2Tapped(sender: UIButton) {
button1.enabled = true
}
}

How to disable button touch at the start in Swift

I have this button:
#IBAction func touch(sender: AnyObject) {
println("Hello, world!")
}
and another:
#IBAction func toggle(sender: AnyObject) {
//code to enable touch of button "touch"
I want to disable user touch of "touch" at the start of the app, and enable touch after tapping "toggle", how to realize it by code and in storyboard?
You need #IBOutlets to access the buttons at runtime. In InterfaceBuilder ctrl-drag the button to the top of your custom class file, choose Outlet and give it a descriptive name. It will look like this:
class MyViewController: UIViewController {
#IBOutlet weak var touch: UIButton!
#IBOutlet weak var toggle: UIButton!
// Disable in `viewDidLoad`:
override func viewDidLoad() {
super.viewDidLoad()
self.touch.enabled = false
}
#IBAction func toggle(sender: AnyObject) {
//code to enable touch of button "touch"
self.touch.enabled = true
}
}
Swift 3 syntax
button.isEnabled = true
An update with swift 2 and IOS 10 is self.touch.isEnabled = false.
They have gotten rid of enabled and replaced it with isEnabled.
You can disable the button in the storyboard - uncheck the enabled checkbox.
To enable it in code you should create an outlet and then set the enabled property to true:
button.enabled = true
Solution 1
myButton.isEnabled = false;
Solution 2 //Desperate way to disable button by disabling the user interaction.
myButton.isUserInteractionEnabled = false;

Resources