1

I have a button inside a custom tableview cell in a tableview. I tried presenting, pushing and instantiating to another view controller but failed by showing error:

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional

Here s my code: console image if required

class ProfileHeaderView: UITableViewCell{

    @IBAction func editProfile(_ sender: Any) {

        let editProfilePage = EditProfileViewController()
        UIApplication.shared.keyWindow?.rootViewController?.present(editProfilePage, animated: true, completion: nil)

    } 

}

The page which i wanted to move:

class EditProfileViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
    override func viewDidLoad() {
        super.viewDidLoad()

        self.hideKeyboardWhenTappedAround()

        mobileNumber.keyboardType = UIKeyboardType.numberPad **//Error**
    }
}

I tried presenting by rootViewController also. Please help.

Screenshot if required

Updated console after the story board identifier change

NiranjanB
  • 59
  • 1
  • 9
  • possible duplicate: [link](https://stackoverflow.com/q/27429652/5567142) – Alok Subedi Jan 25 '18 at 10:01
  • have you tried to show that view controller at all? to test if the issue is related to the tableView or it is an issue with the `EditProfileViewController` only – Milan Nosáľ Jan 25 '18 at 10:07
  • @Milan Nosal.I hope it it is a issue with EditProfileViewcontroller. But why it says those numpad is nil while it was working fine while navigation from slidemenucontroller. – NiranjanB Jan 30 '18 at 04:48
  • @NiranjanB I believe I figured out what's wrong, check my answer – Milan Nosáľ Jan 30 '18 at 08:15

2 Answers2

0

I solved this problem for myself with the following code You can use a protocol to delegate the click inside the tableviewcell back to the UIViewController In my case I did use segues but you can instantiate your EditProfileViewController directly inside the cellCallback function

Code for the delegate

protocol CellDelegator {
    func cellCallback(myData dataobject: Item)
}

Code inside the tableview cell

@IBAction func imageClicked(_ sender: UIButton) {
    self.delegate?.cellCallback(myData: (self.cartItem?.item)!)

}

Code inside the ViewController

extension MyViewController :  CellDelegator{
    func cellCallback(myData dataobject: Item) {
        performSegue(withIdentifier: "xyz", sender: dataobject)
    }
0

EXPLANATION

The crash is caused by line mobileNumber.keyboardType = UIKeyboardType.numberPad not because of UIKeyboardType.numberPad, but rather because of left hand side: mobileNumber.keyboardType. Mobile number is supposed to be an @IBOutlet of type UITextField! (implicitly forced unwrapped UITextField), which causes a crash when mobileNumber is nil. And that is your case.

It was working before, because before you were using a segue to navigate to EditProfileViewController and so storyboards initialized mobileNumber field properly before viewDidLoad was called. However, you are creating an instance of EditProfileViewController programmatically on this line: let editProfilePage = EditProfileViewController(), which totally circumvents storyboards. Therefore fields and actions that should be wired up by storyboards will not get wired up, resulting in your crash.

SOLUTION

Instead of calling an initializer, use storyboards to create an instance of EditProfileViewController. That means, replace this line:

let editProfilePage = EditProfileViewController()

With something like this:

let editProfilePage = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "EditProfileViewController") as! EditProfileViewController

In this line I assume that the storyboard with EditProfileViewController is named Main (of course, if the EditProfileViewController is defined in other storyboard, use that and not the Main storyboard there), plus make sure that you use Storyboard ID in your storyboard and it is set to EditProfileViewController (or use any custom id you want):

Setting storyboard ID

Milan Nosáľ
  • 19,169
  • 4
  • 55
  • 90
  • I tried this also. But showing error: libc++abi.dylib: terminating with uncaught exception of type NSException. – NiranjanB Feb 02 '18 at 07:36
  • @NiranjanB but the error changed, so i believe we are onto something... can you share the whole new message + stacktrace? – Milan Nosáľ Feb 02 '18 at 07:40
  • Thats the complete message after this app hangs there itself. – NiranjanB Feb 02 '18 at 07:51
  • @NiranjanB how about a screenshot of the xcode at that moment? – Milan Nosáľ Feb 02 '18 at 07:56
  • Couldnt add a image to comments section , may i know how ? – NiranjanB Feb 02 '18 at 08:00
  • @NiranjanB either upload it somewhere and provide just link, or add it to the question – Milan Nosáľ Feb 02 '18 at 08:01
  • @NiranjanB see the bottom part of the updated answer and try to use it instead, and then show me the console output – Milan Nosáľ Feb 02 '18 at 08:14
  • TrackAction: left tap open. >> begin >> storyboard: libc++abi.dylib: terminating with uncaught exception of type NSException (lldb) .. – NiranjanB Feb 02 '18 at 08:54
  • @NiranjanB OK, from this I would assume that there is no view controller with storyboard ID = `"EditProfileViewController"` in Main storyboard. 1. check that you are using a correct storyboard - is the `EditProfileViewController` in `Main` or in other? If in other, change storyboard creation to `UIStoryboard(name: "otherStoryboard", bundle: nil)`, if that's not the case, 2. Make sure that you really set the storyboard ID to `"EditProfileViewController"` – Milan Nosáľ Feb 02 '18 at 08:59
  • Nosal. Sorry, that was a mistake, but i changed the identifer, now app is not crashing but at the same time, its not navigating. i have shared the console window. – NiranjanB Feb 02 '18 at 09:11
  • @NiranjanB because you are not presenting it, just creating it.. add the last line: `UIApplication.shared.keyWindow?.rootViewController?.present(editProfilePage, animated: true, completion: nil)` – Milan Nosáľ Feb 02 '18 at 09:13
  • Perfect. Thank you very much, Thank you especially for your patient guidance. Im marking this as Correct answer. – NiranjanB Feb 02 '18 at 09:18