如何实现pickerView类的didSelectRow方法? [英] How to implement the didSelectRow method of the pickerView class?

查看:40
本文介绍了如何实现pickerView类的didSelectRow方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前从未使用过选择器视图.我认为它类似于表视图类的 func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) 方法.但是我不知道如何从选择器值中获取文本并将其显示在标签中.我希望这在 postListing 方法中完成.有人可以帮我解决这个问题吗?谢谢.

I have never worked with a picker view before. I assume it would be similar to the func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) method of the table view class. However I can not figure out how I will get the text from the picker value and display that in a label. I would like this to be done inside the postListing method.Can someone help me out with this please? Thanks.

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 = ""


    }


}

推荐答案

这是从Picker

func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
 {
   println("\(courseCatalog[row])")
    yourlabelname.text=courseCatalog[row]

}

这篇关于如何实现pickerView类的didSelectRow方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆