gpt4 book ai didi

ios - 触摸在Xcode模拟器中无法工作(苹果官方教程 'FoodTracker')

转载 作者:行者123 更新时间:2023-11-29 05:12:41 25 4
gpt4 key购买 nike

我正在编写一个官方 Apple 教程来学习 iOS。

但是我也做了同样的事情,但是触摸在模拟器中不起作用。

我尝试关闭和打开 Xcode,并复制和粘贴示例文件,但它仍然不起作用。或者还有什么你应该尝试的吗?

我需要帮助。

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
}
//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.
dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

// The info dictionary may contain multiple representations of the image. You want to use the original.
guard let selectedImage = info[UIImagePickerController.InfoKey.originalImage] as?
UIImage else {
fatalError("Expected a dictionary containing an image, but was provided folowing: \(info)")
}
// Set photoImageView to display the selected image.
photoimageView.image = selectedImage

// dismiss picker
dismiss(animated: 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
present(imagePickerController, animated: true, completion: nil)
}

@IBAction func setDefaultLabelText(_ sender: UIButton) {
mealNameLabel.text = "Default Text"
}
}

enter image description here

最佳答案

您应该将 isUserInteractionEnabled 添加为 Storyboard 的 true 并添加 Tap Gesture Recognizer 并链接(或更新此链接)@IBAction func selectImageFromPhotoLibrary 与 Storyboard

enter image description here

关于ios - 触摸在Xcode模拟器中无法工作(苹果官方教程 'FoodTracker'),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59531142/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com