繁体   English   中英

Xcode/iOS - 尝试通过简单的打印进行控制台调试,但无法正常工作。 不知道如何找到问题

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

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

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

在此处输入图片说明

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

视图控制器.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"
    }
}

评级控制.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,所以我不确定我是否提供了所有相关信息,所以如果我需要提供更多信息,请告诉我。 谢谢!

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

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

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

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM