最近新学ios开发,按照下面教程中的步骤一步步敲代码,但是在获取图片这块就是触发不了选取图片,反复重来好几遍了,代码又只能看懂大概,我能想到的就是tap gesture没有触发到,不知道为什么,麻烦各位高手给点提示。 https://developer.apple.com/library/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson4.html#//apple_ref/doc/uid/TP40015214-CH6-SW1
//
// ViewController.swift
// FoodTracker02
//
import UIKit
class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
// Mark: Properties
@IBOutlet weak var mealNameLabel: UILabel!
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var photoImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
nameTextField.delegate = self
}
// Mark: UITextFieldDelegate
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func textFieldDidEndEditing(textField: UITextField) {
mealNameLabel.text = textField.text
}
// Mark: UIImagePickerControllerDelegate
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
// Dismiss the picker if user canceld.
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]){
let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
// Set photoImageView to display the selected image
photoImageView.image = selectedImage
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()
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 setDefaultLabel(sender: UIButton) {
mealNameLabel.text = "Defualt Label"
}
}
我看了你的代码,也看了那个教程,我的建议是你不要拘泥那个教程,你添加一个按钮,然后在按钮的事件里面执行selectImageFromPhotoLibrary函数好了
1楼 @tinyfool 我重新仔细又过了一遍教程,发现时我少设置了一步,User Interaction Enabled没有勾选。 谢谢你的建议,基本上我能从函数名大概明白函数是干什么的,但是为什么没有触发函数,这是我遇到的问题。如果说不拘泥于教程,确实我也不知道应该怎么往下走,我的计划是过一遍教程,再去脱离。可能我这人比较笨,就这入门级的熟悉xcode,熟悉界面布局,我都是同时看了各个不同人写的入门教程才看懂,我还参加了StuQ的课程。你看我这光是准备入门就走了这么多道。 还有一个求教的,我看的各种入门教程都是选的Swift的,但是StuQ课程使用的是Object-C。我看了Swift 后 再看Object-C感觉怪怪的。我是不是应该先暂时放弃看Swift的,先学Object-C的。