【发布时间】:2017-04-28 10:26:38
【问题描述】:
我今天开始使用 Apple's Official Guide for Swift Development 开始学习 iOS 开发。
我已联系Work with View Controllers。在页面结束之前有一个检查点,我应该在模拟器上运行应用程序,当我单击图像视图时,我应该得到SIGABRT 错误,因为我没有显示图像的权限。
不仅我没有收到任何错误消息,而且当我单击图像视图时也没有任何反应。
我检查了所有文件 3 次,以确保我没有遗漏任何内容。我什至创建了一个新项目以确保一切正常,但仍然无法正常工作。
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()
// Do any additional setup after loading the view, typically from a nib.
// 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: [String : Any]) {
// The info dictionary may contain multiple representations of the image. You want to use the original.
guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
// Set photoImageView to display the selected image.
photoImageView.image = selectedImage
// Dismiss the 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"
}
}
你能帮我解决一下吗?
【问题讨论】:
-
可能是您的点击手势未从您的故事板连接到您的文件。检查@IBAction func selectImageFromPhotoLibrary(_ sender: UITapGestureRecognizer) 左侧显示的圆圈内部是否填充了灰色。希望对您有所帮助!
-
@PallaviSrikhakollu 我查过了。它显示它已连接到点击手势识别器。
-
那么可能是您的 ImageView 的用户交互或 viewController 的视图已关闭。
标签: ios iphone swift xcode uitapgesturerecognizer