我想在其中输入用户选择的7种语言,然后单击“确定”或“取消”将其设置在标签上。很抱歉要求很多,但我找不到任何好的答案。所有我发现的是关于datepicker我想做同一个页面这个人使用(Showing a UIPickerView with UIActionSheet in iOS8 not working)你可以帮助我plz任何信息提前感谢
答案 0 :(得分:0)
That is the code i used to solve my problem
plus the auto complete for any one having the same problem as me
here is the solution for datepicker with (done,cancel) buttons
hope that helps
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