Swift - UITextField以编程方式无法输入文本

时间:2016-10-17 04:24:40

标签: ios swift uitextfield

我是Swift编程的新手,我正在尝试使用UITextField构建一个应用程序,但是我遇到了问题,我以编程方式创建了一个UITextField但似乎我无法在其中输入文字。这是我的viewDidLoad

代码:

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView = UITableView(frame: self.tableView.frame, style: .Grouped)

    self.groupInfoView.frame = CGRectMake(0, 44, self.view.frame.width, 100)
    self.groupInfoView.backgroundColor = UIColor.orangeColor()
    self.groupInfoView.userInteractionEnabled = true



    groupImg.frame = CGRectMake(10, 50, 40, 40)
    groupImg.layer.cornerRadius = 5
    groupImg.layer.masksToBounds = true
    groupImg.image = UIImage(named: "group-def-img")

    groupTitle = UITextField.init(frame:CGRectMake(groupImg.frame.maxX + 5, 50, self.groupInfoView.frame.width-65, 40))
    groupTitle.backgroundColor = UIColor.whiteColor()
    groupTitle.layer.cornerRadius = 5
    groupTitle.layer.masksToBounds = true
    groupTitle.placeholder = "Enter Group Title"
    groupTitle.textColor = UIColor.orangeColor()
    groupTitle.textAlignment = .Center
    groupTitle.font = UIFont(name: "HelveticaNeue-Light", size: 18)
    groupTitle.autocorrectionType = UITextAutocorrectionType.No
    groupTitle.keyboardType = UIKeyboardType.Default
    groupTitle.returnKeyType = UIReturnKeyType.Done
    groupTitle.clearButtonMode = UITextFieldViewMode.WhileEditing;
    groupTitle.contentVerticalAlignment = UIControlContentVerticalAlignment.Center
    groupTitle.enabled = true
    groupTitle.userInteractionEnabled = true
    groupTitle.delegate = self
    groupTitle.text = "asda"


    recipientLabel.frame = CGRectMake(5, 0, self.view.frame.width - 20, 40)
    recipientLabel.font = UIFont(name: "HelveticaNeue-Light", size: 15)
    recipientLabel.numberOfLines = 3
    recipientLabel.textColor = UIColor.orangeColor()
    recipientLabel.backgroundColor = UIColor.whiteColor()
    recipientLabel.center.x = self.view.center.x
    recipientLabel.layer.cornerRadius = 7
    recipientLabel.layer.masksToBounds = true
    recipientLabel.text = " To:"

    self.groupInfoView.addSubview(groupTitle)
    self.groupInfoView.addSubview(recipientLabel)
    self.groupInfoView.addSubview(groupImg)
    searchBar.delegate = self
    self.titleLabel.text = "New Group Message"
    navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
    navigationController?.navigationBar.shadowImage = UIImage()
    self.navigationController?.tabBarController?.tabBar.barStyle = .Black
    self.navigationController!.navigationBar.translucent = false
    self.navigationController!.navigationBar.barTintColor = UIColor.orangeColor()

    tableView.separatorInset = UIEdgeInsets(top: 0, left: 65, bottom: 0, right: 0)

    tableView.registerClass(UserCell.self, forCellReuseIdentifier: cellId)

    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .Plain, target: self, action: #selector(handleCancel))

    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: .Done, target: self, action: #selector(handleDone))


    fetchUser()
    self.tableView.allowsMultipleSelection = true

    if let navigationBar = self.navigationController?.navigationBar {

        let firstFrame = CGRectMake(0, -5, (self.navigationController?.navigationBar.frame.width)!, 40)
        let secondFrame = CGRectMake(0, 17, (self.navigationController?.navigationBar.frame.width)!, 30)

        titleLabel.frame = firstFrame
        titleLabel.textColor = UIColor.whiteColor()
        titleLabel.font = UIFont(name: "HelveticaNeue-SemiBold", size: 18)
        titleLabel.textAlignment = .Center


        countLabel.textColor = UIColor.whiteColor()
        countLabel.font = UIFont(name: "HelveticaNeue-Light", size: 13)
        countLabel.frame = secondFrame
        countLabel.textAlignment = .Center

        navigationBar.addSubview(groupInfoView)
        navigationBar.addSubview(titleLabel)
        navigationBar.addSubview(countLabel)

    }

And this is my references:

Code:

var titleLabel = UILabel()
var countLabel = UILabel()
var recipientLabel = UILabel()
var groupImg = UIImageView()
var groupTitle: UITextField = UITextField()
var groupInfoView = UIView()

1 个答案:

答案 0 :(得分:0)

全局声明它,以便您可以在任何地方访问它。并声明其委托UITextFieldDelegate Headerimageview是我的心像,HeaderView是我简单的标题视图。

@IBOutlet var Headerimageview: UIImageView!
@IBOutlet var HeaderView: UIView!
var groupTitle : UITextField = UITextField()

viewDidLoad()

groupTitle = UITextField.init(frame: CGRectMake(Headerimageview.frame.size.width + 25, 5, self.view.frame.width-85, 40))
groupTitle.backgroundColor = UIColor.whiteColor()
groupTitle.layer.cornerRadius = 5
groupTitle.layer.borderWidth = 1.0
groupTitle.layer.borderColor = UIColor.brownColor().CGColor
groupTitle.layer.masksToBounds = true
groupTitle.placeholder = "Enter Group Title"
groupTitle.textColor = UIColor.orangeColor()
groupTitle.textAlignment = .Center
groupTitle.font = UIFont(name: "HelveticaNeue-Light", size: 18)
groupTitle.autocorrectionType = UITextAutocorrectionType.No
groupTitle.keyboardType = UIKeyboardType.Default
groupTitle.returnKeyType = UIReturnKeyType.Done
groupTitle.clearButtonMode = UITextFieldViewMode.WhileEditing
groupTitle.contentVerticalAlignment = UIControlContentVerticalAlignment.Center
groupTitle.enabled = true
groupTitle.userInteractionEnabled = true
groupTitle.delegate = self
self.HeaderView.addSubview(groupTitle)

输出:

TextField应该开始编辑名为

的方法

TextField确实开始编辑名为

的方法

输入字符时,此方法称为

输入字符时,此方法称为

输入字符时,此方法称为

TextField应返回名为

的方法

TextField应该使用名为

的编辑方法

TextField执行了名为

的结束编辑方法

使用我的代码更新您的代码...

 groupTitle = UITextField.init(frame:CGRectMake(groupImg.frame.maxX + 5, 25, self.groupInfoView.frame.width-65, 40))
  groupTitle = UITextField.init(frame: CGRectMake(groupImg.frame.size.width + 25, 25, self.view.frame.width-85, 40))
  self.groupInfoView.addSubview(groupTitle)
  self.groupInfoView.addSubview(groupImg)
  self.view.addSubview(groupInfoView)