I've been trying to get a drop down selection menu but running into issues at every turn.
The plan is to have "Select Input" box (label
, text
or button
) and once clicked a popOver acting like a drop down allows the user to select one item from an array as the input.
This version contains a storyboard with 2 view controllers, the main view has only a label
and a button
. the other (Popover) has a tableview
, label
showing the current selection and an ok button.
I've got a working UIPopOver
(See code) that was passing my desired value, but I was unable to get the keyboard to deactivate after the selection was made with a UITextField
.
I've tried switching to a button, with the button label
updating to the selected value on close, but for some reason in this method every value I returned caused an error.
Any my latest attempt was to use a label
with an edit button
next to it, allowing it to switch the label
, but that is returning an optional error that I don't understand.
I am receiving the following error:
on popOverLocation = editedField.frame
"Thread 1: EXC_BAD_INSTRUCTION"
my print line shows "Optional ("test")
followed by " Fatal Error: unexpectedly found nil while unwrapping an Optional value.
I was previously receiving the same error attempting to set TestLabel.text = newValue
, I've confirmed my outlets, but I've never seen this error related to labels before.
Am I going about this the wrong way?
I am open to completely trashing this code and trying another way, but I'm unsure which way to go. My final project will include several of these fields which need options selected from arrays.
(Note: in this version of my code I've moved the UITableviewController
and all code for the popover into a single View Controller along with the main interface. This was done for testing purposes)
My Code Follows:
import UIKit
var newValue = ""
class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {
var arrayofstuff = ["test 1", "test 2", "test 3", "test 4"]
var selection: String = ""
var popOverLocation: CGRect!
@IBOutlet weak var TestLabel: UILabel!
@IBOutlet weak var selectionLabel: UILabel!
@IBOutlet var editedField: UILabel!
@IBOutlet weak var editButton: UIButton!
@IBAction func editButtonAction(sender: AnyObject) {
println(TestLabel.text?)
popOverLocation = editedField.frame
popOver()
}
@IBAction func okButton(sender: AnyObject) {
println(newValue)
TestLabel.text = newValue
self.dismissViewControllerAnimated(false, completion: nil)
}
@IBOutlet var test2: UILabel!
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int
{
return arrayofstuff.count
}
func tableView(tableView: UITableView!,
cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
{
let cell:UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Default, reuseIdentifier:"Cell")
cell.textLabel?.text = arrayofstuff[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let cell:UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Default, reuseIdentifier:"Cell")
var currentrow = indexPath.row
if currentrow == 0 {
selection = arrayofstuff[0]
selectionLabel.text = selection
newValue = selection
println(newValue)
} else if currentrow == 1{
selection = arrayofstuff[1]
selectionLabel.text = selection
newValue = selection
println(newValue)
} else if currentrow == 2{
selection = arrayofstuff[2]
selectionLabel.text = selection
newValue = selection
println(newValue)
} else if currentrow == 3{
selection = arrayofstuff[3]
selectionLabel.text = selection
newValue = selection
println(newValue)
}
}
//function for creating the popover
func popOver() {
//Read the height, and XY coordinates of the active button
var h = popOverLocation.height
var x = popOverLocation.origin.x
var y = popOverLocation.origin.y + h
//create the popover
var popoverContent = self.storyboard!.instantiateViewControllerWithIdentifier("Popover") as UIViewController
var nav = UINavigationController(rootViewController: popoverContent)
nav.modalPresentationStyle = UIModalPresentationStyle.Popover
var popover = nav.popoverPresentationController! as UIPopoverPresentationController
popover.sourceView = self.view
popover.delegate = self
//set the size of the popover
popoverContent.preferredContentSize = CGSizeMake(200,300);
//set the direction the popover arrow is allowed to point
popover.permittedArrowDirections = UIPopoverArrowDirection.Up
//using the buttons coordinates set the location of the popover
popover.sourceRect = CGRectMake(x,y,0,0)
self.presentViewController(nav, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}