I am using image views to display users' friends lists. The user should be able to tap on a friend's icon and be taken to another screen. The code I've written works perfectly on the Xcode simulator. However, when running on a device, the app crashes every single time as soon as I tap an icon.
I really am unsure where to even start debugging this, even after tons of google-ing. Any advice/ help is appreciated.
I have read that my specific error potentially has something to do with memory allocation(?) but still unsure where to start/ what to do. When I check my device logs, the exception type shows this: Exception Type: EXC_BAD_ACCESS (SIGSEGV). I've followed tutorials on finding zombies and it did not help. Thank you.
I'm not really sure what code to post, but since you'll probably want to make sure I am setting up the icons correctly: (i've deleted some code to keep it simple as possible; this is running in a loop to create x amount of icons)
let iv = UIImageView()
iv.translatesAutoresizingMaskIntoConstraints = false
iv.image = image!
iv.tag = j
iv.isUserInteractionEnabled = true
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.iconTapped))
iv.isUserInteractionEnabled = true
iv.addGestureRecognizer(tapGestureRecognizer)
iconTapped function:
func iconTapped(gestureRecognizer: UITapGestureRecognizer, _ sender: AnyObject) {
print("TAPPED NUMBER: \(gestureRecognizer.view?.tag)")
tappedIcon = CurrentSixFriendsList[(gestureRecognizer.view?.tag)!]
let nextVC = ConfirmOpponentViewController()
nextVC.chosenOpponent = tappedIcon
navigationController?.pushViewController(nextVC, animated: true)
}
UPDATE: I've commented out all code on nextVC and all code in iconTapped except for a print statement. The app still crashes on device only when I tap any friend icon.
func iconTapped(gestureRecognizer: UITapGestureRecognizer, _ sender: AnyObject) {
print("tapped")
}
SECOND UPDATE: so i commented out all the code for rendering the image views and I hardcoded an image view. (just one image view) with a static image from my project. the image view displays and the app crashes even when i tap on this imageView. why is it acting so strange!? i'm going crazy... I added this code in VDL just to experiment:
let iv = UIImageView()
iv.isUserInteractionEnabled = true
iv.translatesAutoresizingMaskIntoConstraints = false
iv.image = #imageLiteral(resourceName: "settings icon")
view.addSubview(iv)
iv.widthAnchor.constraint(equalToConstant: 100).isActive = true
iv.heightAnchor.constraint(equalToConstant: 100).isActive = true
iv.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
iv.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(iconTapped))
iv.addGestureRecognizer(tapGestureRecognizer)