-1

So I'm new to iOS Development and I'm finally onto this step: https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/WorkWithViewControllers.html#//apple_ref/doc/uid/TP40015214-CH6-SW1

I've followed the guide step-by-step and for some reason, when I run my app, I click on the Image View and nothing pops up. My code is exactly like Apple's with the exception of nameTextField being businessNameTextField and businessNameLabel. Here's my ViewController.swift file

//
//  ViewController.swift
//  Example (Sample 1)
//
//  Created by lewlsaucengravy on 4/9/17.
//  Copyright © 2017 Example. All rights reserved.
//

import UIKit

class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    //MARK: Properties

    @IBOutlet weak var businessNameLabel: UILabel!
    @IBOutlet weak var businessTextField: UITextField!
    @IBOutlet weak var photoImageView: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        businessTextField.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //MARK: UITextFieldDelegate
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        // Hide the keyboard.
        textField.resignFirstResponder()
        return true
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        businessNameLabel.text = textField.text
    }
    
    //MARK: UIImagePickerControllerDelegate
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        // Dismiss the picker if user cancelled.
        dismiss(animated: true, completion: nil)
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        // The info dictionary may contain multiple representations of the image. You want to use the original.
        guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {
            fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
        }
        
        // Set photoImageView to display the selected image.
        photoImageView.image = selectedImage
        
        // Dismiss the picker.
        dismiss(animated: true, completion: nil)
    }
    
    //MARK: Actions
    @IBAction func completeProfile(_ sender: UIButton) {
        businessNameLabel.text = "test"
    }
    
    @IBAction func selectImageFromPhotoLibrary(_ sender: UITapGestureRecognizer) {
            // Hide the keyboard.
            businessTextField.resignFirstResponder()
            
            // UIImagePickerController is a view controller that lets a user pick media from their photo library.
            let imagePickerController = UIImagePickerController()
            
            // Only allow photos to be picked, not taken.
            imagePickerController.sourceType = .photoLibrary
            
            // Make sure ViewController is notified when the user picks an image.
            imagePickerController.delegate = self
            present(imagePickerController, animated: true, completion: nil)
    }

}

My AppDelegate.swift file is is the default when creating the project, so nothing to show there.

Here's the Main.storyboard:

Sample iOS App

I'm trying to avoid just deleting the app and starting over because I want to get in the habit of troubleshooting and learning what I'm doing wrong.

From what I understand, and feel free to correct me if I'm wrong, when the user clicks on the Image View, it's calling the selectImageFromPhotoLibrary function from which I've defined under Actions, and everything is supposed to go from there.

Edit

It looks like the issue may have been the "User Interaction Enabled" checkbox that I forgot to check. It's the only thing I changed since posting the question and apparently it's working now.

halfer
  • 19,824
  • 17
  • 99
  • 186
LewlSauce
  • 5,326
  • 8
  • 44
  • 91
  • First thing to check: Did you assign a Tap Gesture Recognizer to your `UIImageView`? And did you connect that to `@IBAction func selectImageFromPhotoLibrary()`? Easiest way to check is to put a breakpoint at `businessTextField.resignFirstResponder()` If you tap the image and *do not* reach the breakpoint, that's what you need to fix. – DonMag Apr 09 '17 at 15:47
  • That's weird. So the breakpoint wasn't hit. However, assigning the tap gesture recognizer to the image library is just drag from the object viewer over to the actual image view right? – LewlSauce Apr 09 '17 at 15:56
  • 1
    Check that your image view has User Interaction enabled? – DonMag Apr 09 '17 at 16:03
  • Someone just mentioned that as an answer and I, in fact, did not have it enabled. However, I enabled it and restarted the app simulation and nothing popped up still. – LewlSauce Apr 09 '17 at 16:03
  • In Interface Builder, right-click on your image view... In the popup, see if it *is* connected to the Tap Gesture – DonMag Apr 09 '17 at 16:05
  • Looks like it is. Under "Outlet Connections" there's "gestureRecognizers" on the left and then "Tap Gesture Recognizer" on the right. – LewlSauce Apr 09 '17 at 16:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/141286/discussion-between-donmag-and-lewlsauce). – DonMag Apr 09 '17 at 16:08

2 Answers2

1

Ensure the User Interaction Enabled checkbox is checked:

enter image description here

chengsam
  • 7,315
  • 6
  • 30
  • 38
  • I forgot to enable this checkbox. I enabled it, but I still wasn't able to click on the image or hit the breakpoint that I set at `businessTextField.resignFirstResponder()` – LewlSauce Apr 09 '17 at 16:02
0

From what I am thinking, you did not have set the following parameters in your plist:

enter image description here

J. Doe
  • 12,159
  • 9
  • 60
  • 114
  • I don't see this in my Info.plist file. Is there another area I'm supposed to be able to see this? – LewlSauce Apr 09 '17 at 16:03
  • I think this is your issue :) check out this answer: http://stackoverflow.com/a/39631642/7715250 – J. Doe Apr 09 '17 at 16:04
  • @J.Doe - according to the person asking the question, the code related to the image picker is not even being reached - so it cannot be related to the plist entries. – DonMag Apr 09 '17 at 16:08