How to implement the didSelectRow method of the pickerView class?

和自甴很熟 提交于 2019-12-22 16:38:50

问题


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


    }


}

回答1:


this is the Delegate method of select the components from Picker

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

}


来源:https://stackoverflow.com/questions/29191162/how-to-implement-the-didselectrow-method-of-the-pickerview-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!