您好我已经以编程方式创建了一个视图。我已经添加了标签和文本字段。但是当我点击文本字段时,文本字段不起作用。它甚至没有调用它的委托方法。请帮忙。
in for循环我正在调用此返回方法来创建文本字段
widgetsView.addSubview(createTextFieldForDropDown(entity,ival:i,ypos: posY))
//使用文本字段和标签创建视图的方法
func createTextFieldForDropDown(entity:ListingCategoryFeaturesEntity,ival:Int,ypos:CGFloat) -> UIView
{
var posY = ypos
var posX = CGFloat(0)
let width = (SCREEN_WIDTH/2)-15
var height = CGFloat(40)
let viewRadio = UIView()
viewRadio.frame = CGRectMake(posX, posY, width, height)
viewRadio.backgroundColor = UIColor.greenColor()
height = 40
posY = 0
let lblTitle = UILabel()
lblTitle.frame = CGRectMake(posX, posY, width, height)
lblTitle.text = entity.strDisplayName
lblTitle.backgroundColor = UIColor.redColor()
lblTitle.font = GRAPHICS.FONT_REGULAR(16)
viewRadio.addSubview(lblTitle)
let tag = 9000
posX = lblTitle.frame.maxX+10
var textField = UITextField()
textField.frame = CGRectMake(posX, posY, width, height)
textField = returnTextFieldsProperties(textField, placeHolderText: "", tagValue: tag+ival)
textField.backgroundColor = UIColor.blueColor()
textField.becomeFirstResponder()
viewRadio.addSubview(textField)
return viewRadio
}
//返回文本字段属性的方法
func returnTextFieldsProperties(textField : UITextField ,placeHolderText : String ,tagValue : Int) -> UITextField
{
textField.attributedPlaceholder = NSAttributedString(string:placeHolderText,
attributes:[NSForegroundColorAttributeName: UIColorFromRGB(colorLightGray, alpha: 1.0)])
textField.font = GRAPHICS.FONT_REGULAR(14)
textField.textAlignment = NSTextAlignment.Left
textField.textColor = UIColor.blackColor()
textField.delegate = self
textField.userInteractionEnabled = true
textField.returnKeyType = UIReturnKeyType.Next
let view_left = UIView()
view_left.frame = CGRectMake(20,0,10,textField.frameHeight)
textField.leftView = view_left
textField.leftViewMode = UITextFieldViewMode.Always
textField.borderStyle = UITextBorderStyle.RoundedRect
textField.backgroundColor = UIColor.clearColor()
textField.autocorrectionType = .No
textField.tag = tagValue
return textField
}
答案 0 :(得分:0)
尝试删除:
textField.becomeFirstResponder()
这可能是问题所在,因为您已经标记了所有文本字段,但我想,您想要的只是您点击的文本字段应该是第一响应者。
答案 1 :(得分:0)
我认为您没有设置委托方法,请检查项目中使用的以下内容,
import UIKit
class ViewController: UIViewController, UITextFieldDelegate
如果您创建文本字段,则最初设置委托方法为
var textField = UITextField()
textField.frame = CGRectMake(posX, posY, width, height)
textField = returnTextFieldsProperties(textField, placeHolderText: "", tagValue: tag+ival)
**textField.delegate = self**
textField.backgroundColor = UIColor.blueColor()
viewRadio.addSubview(textField)
希望它有用
答案 2 :(得分:0)
问题是您的textField
不在其超级视图viewRadio
的范围内。使viewRadio
宽度足以容纳标签和textView:
变化:
viewRadio.frame = CGRectMake(posX, posY, width, height)
为:
viewRadio.frame = CGRectMake(posX, posY, SCREEN_WIDTH, height)
答案 3 :(得分:0)
简单的答案,只需编写委托方法并在情节提要中设置UITextField的委托,或在viewDidLoad中手动设置
写这样的东西,它将完美地工作。
list()