gpt4 book ai didi

ios - 如何实现pickerView类的didSelectRow方法?

转载 作者:搜寻专家 更新时间:2023-11-01 05:41:38 25 4
gpt4 key购买 nike

我以前从未使用过选择器 View 。我假设它类似于 TableView 类的 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 = ""


}


}

最佳答案

这是从Picker中选择组件的Delegate方法

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

}

关于ios - 如何实现pickerView类的didSelectRow方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29191162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com