Xcode / iOS - 尝试简单的打印到控制台调试,无法使其工作。不知道如何找到问题

时间:2016-04-12 22:05:18

标签: ios iphone xcode swift debugging

我一直在关注Apple网站上的T:https://developer.apple.com/library/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson5.html#//apple_ref/doc/uid/TP40015214-CH19-SW1

请耐心等待,因为我之前从未问过iOS开发问题(我刚刚开始),但到目前为止这是我的故事板:

enter image description here

触摸(或者在这种情况下,点击)红色方块应该向Xcode调试控制台打印一条消息(参见下面的代码):

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 ")
    }

}

运行模拟器时,单击红色按钮(设置为RatingControl类)不会执行任何操作,也不会向控制台输出任何内容。

我只是刚刚开始使用Xcode / Swift,所以我不确定我是否提供了所有相关信息,所以如果我需要提供更多信息,请告诉我。谢谢!

2 个答案:

答案 0 :(得分:1)

我遇到了与遵循相同教程完全相同的问题。

这就是您所需要的:查看>调试区域>激活控制台。 (cmd + shift + c)

答案 1 :(得分:1)

对于XCode v11 + SwiftUI用户,如果发现原始海报解释的问题,您必须在Debugging中激活Live preview。这是一个示例,假设您拥有view > debug area > activate console

enter image description here