0

So I have an array made of strings, and when the user presses on my textfield, it shows a PickerView using the array.

I would like the PickerView to show the contents of the array in alphabetical order. But I have no idea how to do this, and cannot find anywhere online how to do it in Swift. Can someone please help?

Thanks in advance!

MY CODE:

import UIKit

class ViewController: UIViewController, UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate {

@IBOutlet var questionLabel: UILabel!

@IBOutlet var buttonLabel: UIButton!

@IBOutlet var myBackgroundView: UIImageView!

@IBOutlet var questionTextField: UITextField!

let questions = ["Where are you going?", "Which city?", "When do you go?"]

var currentQuestionIndex = 0

let placeholder = ["Country", "City", "Date"]

var currentPlaceholderIndex = 0

@IBAction func nextButton(sender: AnyObject) {

// Initial setup on button press
questionTextField.hidden = false

barImage.hidden = false

// Reset text field to have no text
questionTextField.text = ""

// Displays the questions in array and displays the placeholder text in the textfield
if currentQuestionIndex <= questions.count && currentPlaceholderIndex <= placeholder.count {

    questionLabel.text = questions[currentQuestionIndex]
    questionTextField.placeholder = placeholder[currentPlaceholderIndex]

    currentQuestionIndex++

    currentPlaceholderIndex++

    buttonLabel.setTitle("Next", forState: UIControlState.Normal)


    // Animate text for questionLabel
    UIView.animateWithDuration(1.0, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.5, options: nil, animations: {

        self.questionLabel.center = CGPoint(x: -110 , y: 305 + 20)

        }, completion: nil)

} else {

    performSegueWithIdentifier("countdownSegue", sender: self)

    //Add some logic here to run whenever the user has answered all the questions.

}

}

var countryPicker = ["France", "Germany", "Spain", "Northern Ireland", "Austria"]

var sortedArray = sorted(countryPicker)


override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

// Hides the text field
questionTextField.hidden = true

questionTextField.delegate = self

// Sets the button text
buttonLabel.setTitle("Get started", forState: UIControlState.Normal)

// Sets the question text to be blank
questionLabel.text = ""

// Sets placeholder text in the text field
questionTextField.placeholder = ""

var pickerView = UIPickerView()

pickerView.delegate = self

questionTextField.inputView = pickerView


}

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return countryPicker.count
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return countryPicker[row]
}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
questionTextField.text = countryPicker[row]
}



// resigns the keyboard when user presses the return/next key on keyboard

func textFieldShouldReturn(textField: UITextField) -> Bool {

questionTextField.resignFirstResponder()

return true

}

// Resigns the keyboard if the user presses anywhere on the screen
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

self.view.endEditing(true)
}


override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}
7
  • Similar to stackoverflow.com/questions/25223058/…, maybe that'll be enough to get you started Commented May 17, 2015 at 22:02
  • I see 2 strange things about the new code you've posted. You're declaring new instance variables in-between the definitions of your instance methods. Don't do that. Define your instance variables at the top of your class, before defining your functions. It's too confusing otherwise, and it's illegal Objective-C. Commented May 18, 2015 at 17:23
  • Second, you have you code to create the sorted version not in any function. I guess that's legal since it is part of the declaration of your variable, but again, it's confusing. Commented May 18, 2015 at 17:25
  • @DuncanC - What do you mean it's illegal Objective-C? I'm not coding in objective C. I'm coding in Swift. Commented May 18, 2015 at 20:36
  • I understand that. My point is that doing that is going to confuse the living crap out of Objective-C programmers (The bulk of Mac & iOS developers) Commented May 18, 2015 at 20:54

2 Answers 2

3

Sort the array, or create a sorted version of the array, and then use that sorted array as the source for your picker view.

let array = ["orange", "grape", "apple", "pear", "mango"]
let sortedArray = sorted(array)

you would then use sortedArray in the data source for your picker view.

Sign up to request clarification or add additional context in comments.

6 Comments

Hi @DuncanC - I have put your code in, but I get an error on the second sortedArray line, that says "'ViewController.Type' does not have a member named 'array'" - do you know why this might be? thanks
Well, sure. If you have an array and you want to sort it you need to use the name of your array instead of "array". If your array is called "foobar" then you would enter let sortedArray = sorted(fooBar)
Hi, Sorry, I did use my own array, it's called 'countryPicker' So i did: let sortedArray = sorted(countryPicker) , I have checked its definitely spelt correctly but i still get the error as mentioned above - 'ViewController.Type' does not have a member named 'countryPicker'
Impossible to say without seeing your code. Edit your original question to show your new code. (at the bottom as new text - don't edit the original question)
You posted a bunch of code but I don't see any sorting code. How about indicating which lines are giving you a problem? (The code you posted does not reference countryPicker at all.)
|
0

I managed to get was i was looking for with this:

countryPicker.sort(){$0 < $1}

It sorted the array from A-Z, and was the only way it worked for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.