-1

I've been working on a hard-coded language switcher for my app, where I have a xib file, where when I tap on a cell, it changes the language of the app, however I have a label that has the text "Welcome to" which will not register at all. In the elements inspector tool, I have added the text "Welcome to" (I even edited the font for reference) and every time I try to edit the title in any way I get the error - "Fatal error: Unexpectedly found nil while unwrapping an Optional value". I am begging you guys for a fix!

here is how it looks:

class ViewController: UIViewController {

    @IBOutlet weak var WelcomeTo: UILabel?
    @IBOutlet var Language: UILabel?
...
/// *when the cell is tapped on, this registers*
func valueInit(WelcomeToString: String, LanguageString: String) {
    
    var testValue = WelcomeToString
    
    testValue = (WelcomeTo?.text!)!
 
    print(testValue)
}
  • Im begging you, do not ask "IS IT CONNECTED!". My outlet is more attached to my view controller than my arm is to my body so I'm begging you not to even try asking...

  • Tried removing and adding "Weak" before the outlet. Didn't help at all.

  • Tried adding the languages to my main file (instead of holding them in another file). Didn't help at all

  • Tried every solution here - IBOutlet is nil, but it is connected in storyboard, Swift. Didn't help at all

7
  • Note that the standard way to declare outlet properties is to use an implicitly unwrapped optional, eg @IBOutlet weak var WelcomeTo: UILabel!. Maybe not related to your issue but you don’t have to force unwrap the property every time you access it if you declare it like this. Commented Jun 1 at 6:27
  • (WelcomeTo?.text!)! will crash if WelcomeTo is nil or its text is nil. Please clarify which one it is: print("label: \(WelcomeTo)"); print("text: \(WelcomeTo?.text)") Your label might be correctly connected, but maybe you don't init the ViewController correctly, ie not with the xib and its outlets.
    – Larme
    Commented Jun 1 at 6:42
  • @Larme, im looking to access the Welcome.text label (which I think Is that second option of print("text: (WelcomeTo?.text)") ) however I cannot seem to do it.
    – Mous772
    Commented Jun 1 at 8:49
  • Did you have a crash then? On which one exactly? print("label: \(WelcomeTo!)") with a question mark if needed.
    – Larme
    Commented Jun 1 at 8:57
  • 1
    It is not enough to have the outlet connected between the label in the XIB and the viewcontroller class. You must also ensure that the view controller instance was created from the XIB. How do you create the view controller instance?
    – Paulw11
    Commented Jun 2 at 1:07

1 Answer 1

-1

I want to start off by saying this was one of the most cooked coding experiences yet 😤.

So let's solve this. For this I'll be using a XIB file, and a view controller (as shown below).

For my cells in the tableview, I found I could transfer values between files, but was however unable to change values between the files. The solution I found was simple, I just made my view controller have an extension of uitableviewdelegate (not forgetting to write "tableView.delegate = self" in viewdidload). Next I wrote the following

this is only for added context, you don't need to add it /////////////////////////////////////////////////////

 //MARK: - tableViewData
    extension ViewController: UITableViewDataSource {
        
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return cells.count
        }
        
        ///loaded when table view loads up
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "ReusableCell2", for: indexPath) as! NewTableViewCell
            cell.languageText.text  = cells[indexPath.row].title
            
            return cell
        }
    }

add what's below (with your own values of course)

extension ViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {   
        WelcomeTo?.text = cells[indexPath.row].welcomeToTextBar
        Language?.text = cells[indexPath.row].languageTextBar
}

I sourced the values from my xib file into a collect called "cells", where I made ".languageTextBar" and ".welcomeToTextBar" have some pre-set values.

Not the answer you're looking for? Browse other questions tagged or ask your own question.