I want to input in it a 7 languages that the user pick then click OK or Cancel to set it on a label. Sorry for asking a lot but I couldn't find any good answers. All I found was about datepicker i want to do the same page this guy using (Showing a UIPickerView with UIActionSheet in iOS8 not working) can u help me plz with any info thanks in advance
That is the code I used to solve my problem plus the autocomplete for anyone having the same problem as me. Here is the solution for datepicker with (done,cancel) buttons
var picker = UIPickerView()
var autocompleteUrls = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// The Auto Compelete Delegates
DatePickerTextOutLet.delegate = self
NationalityOutLet.delegate = self
AutoComTable.delegate = self
AutoComTable.dataSource = self
AutoComTable.scrollEnabled = true
AutoComTable.hidden = true
} // ViewdidLoad
///////////////////////////////////Language picker
func PickerSensei(textField:UITextField){
let picker: UIPickerView
picker = UIPickerView(frame: CGRectMake(0, 200, view.frame.width, 200))
picker.backgroundColor = .whiteColor()
picker.showsSelectionIndicator = true
picker.delegate = self
picker.dataSource = self
let toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.Default
toolBar.translucent = true
toolBar.tintColor = UIColor(red: 226/255, green: 0/255, blue: 0/255, alpha: 1)
toolBar.sizeToFit()
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "donePicker")
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
// let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "CancelPicker")
toolBar.setItems([ spaceButton, doneButton], animated: false)
toolBar.userInteractionEnabled = true
textField.inputView = picker
textField.inputAccessoryView = toolBar
}
func donePicker() {
PickerSensei(FirstnameoutLet)
if ((FirstnameoutLet.text?.characters.count) < 1 ){
FirstnameoutLet.text = "Arabic"
}
FirstnameoutLet.resignFirstResponder()
}
func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
let attributedString = NSAttributedString(string: PreferedLanguagesResult[row], attributes: [NSForegroundColorAttributeName : UIColor.redColor()])
return attributedString
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
return 1
}
// returns the # of rows in each component..
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
return PreferedLanguagesResult.count
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
FirstnameoutLet.text = String(PreferedLanguagesResult[row])
// FirstnameoutLet.endEditing(true)
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return self.PreferedLanguagesResult[row]
}
//////////////////////// end of Langauge picker
//////// Date Picker
@IBAction func DatePickeractionButton(sender: AnyObject) {
}
func textFieldDidBeginEditing(textField: UITextField) {
datePicker.datePickerMode = UIDatePickerMode.Date
DatePickerTextOutLet.inputView = datePicker
// datePicker.addTarget(self, action: "datePickerChanged:", forControlEvents: .ValueChanged)
BirthDate = DatePickerTextOutLet.text!
let toolBar = UIToolbar()
toolBar.barStyle = .Default
toolBar.translucent = true
toolBar.tintColor = UIColor(red: 216/255, green: 0/255, blue: 0/255, alpha: 1)
toolBar.sizeToFit()
datePicker.setValue(UIColor.redColor(), forKeyPath: "textColor")
datePicker.backgroundColor = UIColor.whiteColor()
// Adds the buttons
let doneButton = UIBarButtonItem(title: "Done", style: .Plain, target: self, action: "doneClick")
let spaceButton = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: .Plain, target: self, action: "cancelClick")
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.userInteractionEnabled = true
// Adds the toolbar to the view
DatePickerTextOutLet.inputView = datePicker
DatePickerTextOutLet.inputAccessoryView = toolBar
// print(BirthDate)
}
func doneClick() {
print(datePicker.date.age)
if ((datePicker.date.age) >= 9460800 && (datePicker.date.age) <= 42048000 ){
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = .ShortStyle
dateFormatter.dateFormat = "dd/MM/yyyy"
DatePickerTextOutLet.text = dateFormatter.stringFromDate(datePicker.date)
DatePickerTextOutLet.resignFirstResponder()
}
else if ((datePicker.date.age) <= 9460800 ){
alertWithTitle("Go Home", message: "You are To young my Child", ViewController: self, toFocus: DatePickerTextOutLet)
} //younger than 18 Years Old
else if ((datePicker.date.age) >= 42048000 ){
alertWithTitle("Sorry Sir", message: "You are to Old for This Shit", ViewController: self, toFocus: DatePickerTextOutLet)
}// older than 80 Years Old
}
func cancelClick() {
DatePickerTextOutLet.resignFirstResponder()
}
///////// Date Picker End
/////////////////// that as for the picker now for the auto complete :-
//////////////////////// The Begining of Auto Compelete
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
AutoComTable!.hidden = false
let text = NationalityOutLet.text ?? ""
let substring = ( text as NSString ).stringByReplacingCharactersInRange(range, withString: string )
searchAutocompleteEntriesWithSubstring(substring)
return true
}
func searchAutocompleteEntriesWithSubstring(substring: String)
{
autocompleteUrls.removeAll(keepCapacity: false)
// print(substring)
for curString in NationalityIDResults
{
// print(curString)
let myString: NSString! = curString as! NSString
let substringRange: NSRange! = myString.rangeOfString(substring)
if (substringRange.location == 0)
{
autocompleteUrls.append(String(curString))
}
}
AutoComTable.reloadData()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return autocompleteUrls.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let autoCompleteRowIdentifier = "AutoCompleteRowIdentifier"
var cell = tableView.dequeueReusableCellWithIdentifier(autoCompleteRowIdentifier) as UITableViewCell!
// print(cell)
if let tempo1 = cell
{
let index = indexPath.row as Int
cell.textLabel!.text = autocompleteUrls[index]
} else
{
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: autoCompleteRowIdentifier)
}
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let selectedCell : UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
// var currentTextField = textField.text
NationalityOutLet.text = selectedCell.textLabel!.text
AutoComTable.hidden = true
}
////////////////////////////////////////// The End of Auto Compelete