Button enabled/disabled

Updated 6 days ago in Swift 3
6 on February 13, 2018

Hello, I am a beginner in Swift. My question: I have 2 buttons. How can I enable 1 button with the other? read a lot on Internet but I don’t understand. Can someone explain me step by step?

 

Gr.Edje

 
  • Liked by
Reply
3 6 days ago

Alright, I’ll try to explain it to you as good as possible!

Step 1 – Create 2 Buttons:

Get both UIButtons onto your Storyboard. I suggest to split your xCode screen with the Button in your top right corner which has two rings on it. On the left I usually have the Storyboard and on the right the ViewController.

Step 2 – Add Outlets:

Drag and drop by pressing ‘Ctrl’ and Mouse Leftclick from your UIButtons to the ViewController. Name your UIButton and make sure the Connection says ‘Outlet’. Repeat that with the other button (Different names!). Here you’ll find what i mean with ‘Drag and Drop’ with images: http://rshankar.com/different-ways-to-connect-ibaction-to-uibutton/

Step 3 – Create IBAction:

Again Drag and drop from a Button onto the ViewController but this time change the Connection to ‘Action’. Change the Type from ‘any’ to ‘UIButton’ and name your Function. I named it ‘enableDisableButton’.

Step 4 – Add both Buttons:

Right now you only connected one Button to this function. Drag and drop again from the other Button ONTO your created Function from the last Step! You can rightclick on both of your Buttons and on ‘Touch Up Inside’ it should say that they are connected to the function.

Step 5 – Code your Function:

Here you have an example of what my ViewController looks like:

import UIKit

class ViewController: UIViewController {

    

    @IBOutlet weak var bLeft: UIButton!

    @IBOutlet weak var bRight: UIButton!

    

    @IBAction func enableDisableButton(_ sender: UIButton) {

        if sender == bLeft {

            bLeft.isEnabled = false

            bRight.isEnabled = true

        }

        else if sender == bRight {

            bLeft.isEnabled = true

            bRight.isEnabled = false

        }

    }

    

    override func viewDidLoad() {

        super.viewDidLoad()

    }

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

    }

}

Function explained: If you click on a Button it checks which one is clicked (if SENDER == bLeft or SENDER == bRight), the code is a bit sloppy but it’s easier to understand it like this i hope. If one of these two buttons is pressed it changes the clicked one to .isEnabled = false and the other one to .isEnabled = true.

Hope this helps! If you have any Questions, please ask. 

6 days ago
6 days ago

Hoi Ben, thanx a lot. Its finally clear to me. A good start for the day.

6 days ago

It’s always a good day as an application developer to be able to go home after work and say ‘I’ve learned something new’. When I started I watched a video of a Microsoft Developer and he said your goal for each day should be to learn something new each day. Even if it’s something small. Keep it up! Unfortunately I’m not much of a Swift programmer anymore because many companies switched to ‘Xamarin’ to create apps for different mobile devices with different OS. Back to C# it is!

Show more replies
  • Liked by
Reply
Cancel
0 6 days ago

I agree with “learned something new”. Learned a lot from Sebastian (The Swift Guy) and his video’s on Youtube. For the moment I stay by Xcode and Swift. Maybe you can help me with this.

 

var eersteArray = “1. Bread”, “2. Milk”, “3. Sugar”

var tweedeArray: [String] = [ ]

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

       tableView.deselectRow(at: indexPath, animated: true)

A = eersteArray[indexPath.row]

tweedeArray.append(A) }

Everything OK and I get the value for A

2.

Now I have a tableview with sections . Everything else the same

I get this error

A = eersteArray[indexPath.row]     Cannot assign value of type ‘[String]’ to type ‘String’

 

 

 

  • Liked by
Reply
Cancel
0 6 days ago

Forget the question.Problem solved.

Thanx.

 

  • Liked by
Reply
Cancel
Loading more replies