user3183717
user3183717

Reputation: 4667

Xcode/iOS - Trying a simple print to console debug, can't get it to work. No clue how to find the issue

I've been following the tutorial to the T on Apple's website: https://developer.apple.com/library/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson5.html#//apple_ref/doc/uid/TP40015214-CH19-SW1

Bear with me as I've never asked an iOS development question on SO before (I'm only just starting out), but here is my storyboard so far:

enter image description here

Touching (or in this case, clicking) the red square should print a message to the Xcode debug console (see code below):

ViewController.swift:

    import UIKit

class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    // MARK: Properties
    @IBOutlet weak var nameTextField: UITextField!
    @IBOutlet weak var mealNameLabel: UILabel!
    @IBOutlet weak var photoImageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Handle the text field’s user input through delegate callbacks.
        nameTextField.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) {
        mealNameLabel.text = textField.text
    }
   // MARK: UIImagePickerControllerDelegate
    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        // Dismiss the picker if the user canceled.
        dismissViewControllerAnimated(true, completion: nil)
    }
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        // The info dictionary contains multiple representations of the image, and this uses the original.
        let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
        // Set photoImageView to display the selected image.
        photoImageView.image = selectedImage
        // Dismiss the picker.
        dismissViewControllerAnimated(true, completion: nil)
    }
   // MARK: Actions

    @IBAction func selectImageFromPhotoLibrary(sender: UITapGestureRecognizer) {
        // Hide the keyboard.
        nameTextField.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
        presentViewController(imagePickerController, animated: true, completion: nil)
    }
    @IBAction func setDefaultLabelText(sender: UIButton) {
        mealNameLabel.text = "Default Text"
    }
}

RatingControl.swift:

import UIKit

class RatingControl: UIView {

   // MARK: Initialization
    required init?(coder aDecoder: NSCoder) {

        super.init(coder: aDecoder)

        let button = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))

        button.backgroundColor = UIColor.redColor()

        button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(_:)), forControlEvents: .TouchDown)

        addSubview(button)
    }
    override func intrinsicContentSize() -> CGSize {
        return CGSize(width: 240, height: 44)
    }

    // MARK: Button Action
    func ratingButtonTapped(button: UIButton) {
        print("Button pressed 👍")
    }

}

When running the simulator, clicking on the red button (which is set to the class RatingControl) does nothing, nor prints anything to the console.

I'm only just starting Xcode/Swift so I'm not sure if I've provided all the relevant info, so please let me know if there's any more info I need to provide. Thank you!

Upvotes: 0

Views: 2542

Answers (2)

punkbit
punkbit

Reputation: 7707

For XCode v11 + SwiftUI users who find the issue the original poster explained you'll have to activate Debugging in your Live preview. Here's an example, assuming that you have the view > debug area > activate console:

enter image description here

Upvotes: 1

Will Son
Will Son

Reputation: 31

I had the exactly the same issue as following the same tutorial.

This is what you need: View > Debug Area > Activate Console. (cmd + shift + c)

Upvotes: 1

Related Questions