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.
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)
}
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):
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.