簡體   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