Learn Courses My Dashboard

Toggle switch to show and hide button

Hi community,

In my app, if a button is pressed, it segues to my ‘Schedule Recording’ Controller.

For additional functionality, I tried adding a switch toggle to show / hide my button depending on whether the user wants to enable / disable the function.

I then added this function which successfully show / hides my button

However, now when I click the button, it crashes and doesn’t segue to the TimeViewController.

What am I doing wrong?

Can you show the code for your ViewController in which you have created the Button?

Paste your code in as text, rather than providing a screenshot.

To format the code nicely, place 3 back-ticks ``` on the line above your code and 3 back-ticks ``` on the line below your code. Like this:

```
Code goes here
```

The 3 back-ticks must be the ONLY characters on the line. The back-tick character is located on the same keyboard key as the tilde character ~ (which is located below the Esc key - QWERTY keyboard).

Alternatively after you paste in your code, select that code block and click the </> button on the toolbar. This does the same thing as manually typing the 3 back ticks on the line above and below your code.

This also makes it easier for anyone assisting as they can copy the code and carry out some testing.

Hi Chris, yes absolutely.

import UIKit
import AVFoundation
import CoreLocation
import SPPermissions
import Accelerate

class RecordSoundsViewController: UIViewController,CLLocationManagerDelegate  {
        
    var logMessages = [
        "System initialised"
    ]
        
    //file the recording will go into
    var audioFile: AVAudioFile?
    
    let engine = AVAudioEngine()
    
    //name of the recording
    var wavFileName = ""
    
    //the number of the recording
    private var recordingNumber = 0
    
    //wether recording is taking place
    var recording = false
    
    //time recording started
    private var recordingStartTime: Double = 0
    private var recordingStartTimeFixed: Double = 0
    
    //recording button
    @IBOutlet weak var recordAudioBtn: UIButton!
    
    //the audio visualisation
    @IBOutlet weak var recordPlot: AudioVisualizerView!
    
    //How long recording has been going
    @IBOutlet weak var timeLabel: UILabel!
    
    @IBOutlet weak var buttonStackView: UIStackView!
    
    @IBOutlet var tableView: UITableView!
    
    @IBOutlet weak var existingFilesCount: UITextField!
    
    @IBOutlet weak var avaliableSpace: UITextField!
    
    //button for schedule recording
    @IBOutlet weak var scheduleRecordingBtn: UIButton!
    
    struct AppUtility {

        static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
        
            if let delegate = UIApplication.shared.delegate as? AppDelegate {
                delegate.orientationLock = orientation
            }
        }

    }
    
    /* Hides the schedule-recording button when schedule-recording toggle switch
     is disabled in settings */
    override func viewDidAppear(_ animated: Bool) {
        //Hide the Schedule Recording Button
        if(!UserDefaults.standard.bool(forKey: "schedule recording")){
            self.scheduleRecordingBtn.isHidden = true
    }
        else{
            self.scheduleRecordingBtn.isHidden = false
        }
    }
'''

OK so you have connected your scheduleRecordingBtn to your storyboard RecordSoundsViewController.

Did you at any point rename any of your outlets by just editing the name of the outlet in RecordSoundsViewController rather than delete it and then re-create the connection from the storyboard RecordSoundsViewController?

What I am getting at is that you may have a disconnected remnant outlet connection between the swift file and the storyboard.

To check that select the ViewController in your storyboard and then in your Inspector panel on the right select the “Connections Inspector”. Look for any outlet that have an amber triangle next to them which means that there is a disconnected outlet between the storyboard and the ViewController swift file. Screenshot example below:

To fix that you can delete the Outlet in the ViewController.swift file and then re-create the outlet by Control dragging the button to your ViewController but you will have to manually delete the orphaned connection in the storyboard by tapping on the x to the left of the amber triangle. See the next screenshot.

Omy!! That fixed it!! Thank you so much Chris :)) was going nuts tryna resolve this.

You’re right, I did rename the outlet assuming that it would automatically change it all for me.

Again, thanks so much! This community has really helped me move forward with developing my project.

If you ever need to change the name of an outlet or a variable in the future, use the Refactor > Rename facility by right clicking on the property and select Refactor > Rename.

Xcode will gather up al the appearances of that property and show them all is a split screen View. Change the name of the one that has the coloured border around it (the one you used to trigger the rename) and then tap on the Rename button at the top the editing window and all of the appearances will be renamed. Voila!

1 Like