我之前从未使用过选择器视图。我假设它类似于表视图类的func tableView(tableView:UITableView,didDeselectRowAtIndexPath indexPath:NSIndexPath)方法。但是我无法弄清楚如何从拾取器值中获取文本并在标签中显示该文本。我希望这可以在postListing方法中完成。有人可以帮我解决这个问题吗?谢谢。
import UIKit
class BooksViewController: UIViewController , UIPickerViewDelegate, UIPickerViewDataSource, UIImagePickerControllerDelegate , UINavigationControllerDelegate {
@IBOutlet var bookImage: UIImageView!
@IBOutlet var coursePicker: UIPickerView!
@IBOutlet var bookDescription: UITextField!
@IBOutlet var pickerSelectionLabel: UILabel!
var photoSelected = false
var activityIndicator:UIActivityIndicatorView = UIActivityIndicatorView()
@IBAction func pickImage(sender: AnyObject) {
println("pick image pressed")
var image = UIImagePickerController()
image.delegate = self
image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
image.allowsEditing = false
self.presentViewController(image, animated: true, completion: nil)
}
@IBAction func postListing(sender: AnyObject) {
var error = ""
if (photoSelected == false) {
error = "Please select an image."
} else if (bookDescription.text == "") {
error = "Please enter a description"
}
if (error != "") {
displayAlert("Can't Post Image", error: error)
} else {
let actIndPoint = CGPointMake(self.view.center.x, self.view.center.y)
activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50))
activityIndicator.center = actIndPoint
activityIndicator.hidesWhenStopped = true
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
view.addSubview(self.activityIndicator)
activityIndicator.startAnimating()
UIApplication.sharedApplication().beginIgnoringInteractionEvents()
var bookPosting = PFObject(className: "BookPosting")
bookPosting["Description"] = bookDescription.text
bookPosting["username"] = PFUser.currentUser().username
//bookPosting["subject"] = coursePicker
bookPosting.saveInBackgroundWithBlock({ (success: Bool!, error: NSError!) -> Void in
self.activityIndicator.stopAnimating()
UIApplication.sharedApplication().endIgnoringInteractionEvents()
if (success == false) {
self.displayAlert("Can't Post Image", error: "Please try again")
} else {
let imageData = UIImagePNGRepresentation(self.bookImage.image)
let imageFile = PFFile(name: "image.png", data: imageData)
bookPosting["imageFile"] = imageFile
bookPosting.saveInBackgroundWithBlock({ (success: Bool!, error: NSError!) -> Void in
if (success == false) {
self.displayAlert("Can't Post Image", error: "Please try again")
} else {
println("Successful post!")
self.displayAlert("Your image has been posted", error: "Posted Successfully")
self.photoSelected = false
self.bookImage.image = UIImage(named: "book1.png")
self.bookDescription.text = ""
}
})
}
})
}
println("post listing pressed")
}
func displayAlert(title: String, error: String) {
var alert = UIAlertController(title: title, message: error, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { action in
self.dismissViewControllerAnimated(true, completion: nil)
}))
self.presentViewController(alert, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
self.dismissViewControllerAnimated(true, completion: nil)
bookImage.image = image
photoSelected = true
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return courseCatalog.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return courseCatalog[row]
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
self.view.endEditing(true)
}
override func viewDidLoad() {
println("Books view controller")
super.viewDidLoad()
photoSelected = false
bookImage.image = UIImage(named: "book1.png")
bookDescription.text = ""
}
}
答案 0 :(得分:0)
这是从Delegate
Picker
方法
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
{
println("\(courseCatalog[row])")
yourlabelname.text=courseCatalog[row]
}