import UIKitclass VIEwController: UIVIEwController,UIPickerVIEwDataSource,UIPickerVIEwDelegate { // Do any additional setup after loading the vIEw,typically from a nib. //initialization constants let pickerVIEw = UIPickerVIEw() let textFIEld = UITextFIEld() let pickerData = ["1","2","three"] overrIDe func vIEwDIDLoad() { //pickervIEw tool bar let toolbar = UIToolbar(frame: CGRectMake(0,320,44)) var items = [AnyObject]() //making done button let donebutton = UIbarbuttonItem(Title: "Done",style: .Plain,target: self,action: Selector(donepressed())) items.append(donebutton) toolbar.barStyle = UIbarStyle.Black toolbar.setItems(items,animated: true) //creating textfIElds with a pickervIEw pickerVIEw.delegate = self pickerVIEw.dataSource = self pickerVIEw.frame = CGRectMake(0,500,300) textFIEld.inputAccessoryVIEw = toolbar textFIEld.inputVIEw = pickerVIEw textFIEld.frame = CGRectMake(200,55,100,35) textFIEld.backgroundcolor = UIcolor.bluecolor() //adding obJs to vIEwController self.vIEw.addSubvIEw(textFIEld)}func donepressed() { resignFirstResponder()}overrIDe func dIDReceiveMemoryWarning() { super.dIDReceiveMemoryWarning() // dispose of any resources that can be recreated.}}//MARK: Data Sources UIPickerVIEwextension VIEwController: UIPickerVIEwDataSource { func numberOfComponentsInPickerVIEw(pickerVIEw: UIPickerVIEw) -> Int { return 1 } func pickerVIEw(pickerVIEw: UIPickerVIEw!,numberOfRowsInComponent component: Int) -> Int { return pickerData.count }}//MARK: Delegates UIPickerVIEwextension VIEwController: UIPickerVIEwDelegate { // several optional methods:func pickerVIEw(pickerVIEw: UIPickerVIEw,TitleForRow row: Int,forComponent component: Int) -> String! { return pickerData[row]}func pickerVIEw(pickerVIEw: UIPickerVIEw!,dIDSelectRow row: Int,inComponent component: Int) { textFIEld.text = pickerData[row]}}有两个问题:
>对于donepressed的选择器声明不正确.它应该是:
let donebutton = UIbarbuttonItem(Title: "Done",action: "donepressed")
>您需要在textFIEld上调用resignFirstResponder():
func donepressed() { textFIEld.resignFirstResponder()}
如果您有多个textFIEld,并且您不想在resignFirstResponder调用中对textFIEld进行硬编码,则可以执行以下 *** 作:
>使您的VIEwController成为UITextFIEldDeletate:
class VIEwController: UIVIEwController,UIPickerVIEwDelegate,UITextFIEldDelegate {
>在UITextFIEld上设置delegate属性:
textFIEld.delegate = self
>将一个属性添加到VIEwController以跟踪活动的UITextFIEld:
var activeTextFIEld:UITextFIEld?
>实现textFIEldDIDBeginEditing并存储活动的UITextFIEld:
func textFIEldDIDBeginEditing(textFIEld: UITextFIEld) { // became first responder activeTextFIEld = textFIEld}
>在donepressed中,调用activeTextFIEld上的resignFirstResponder:
func donepressed() { activeTextFIEld?.resignFirstResponder()}总结
以上是内存溢出为你收集整理的UITextField Swift中的ios – resignFirstResponder全部内容,希望文章能够帮你解决UITextField Swift中的ios – resignFirstResponder所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)