以编程方式添加UITextField - 键盘导致应用程序在第一次按键时崩溃 - Swift

时间:2016-01-29 19:25:50

标签: ios swift uitableview uitextfield

我有一个“TextInputCell”类,它继承自UITableViewCell,表示包含UITextField的UITableViewCell。在View Controller中使用此类将“TextInputCell”添加到UITableView的一行时,我将委托设置为View Controller本身,并且委托函数为空(除了return语句) - 我将在下面发布。运行应用程序时,文本字段完全按照我的要求显示,并允许我在文本字段中单击并弹出键盘,但是第二个我按下键盘上的任何键应用程序崩溃错误 DataContext="{Binding RelativeSource={RelativeSource Self}}"> <Grid x:Name="LayoutRoot"> <ListView ItemsSource="{Binding Items}" listViewLayout:ListViewLayoutManager.Enabled="true" x:Name="Lv"> <ListView.View> <GridView > <GridView.Columns> <GridViewColumn Header="Location" listViewLayout:ProportionalColumn.Width="1" DisplayMemberBinding="{Binding Location}"/> <GridViewColumn Header="Name" listViewLayout:ProportionalColumn.Width="1" DisplayMemberBinding="{Binding Name}"/> </GridView.Columns> </GridView> </ListView.View> </ListView> </Grid> 。我尝试将一个常规的UITextField添加到没有我制作的类的单元格中,我得到了同样的错误。有谁知道为什么会发生这种情况或我如何解决它?

TextInputCell类:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull length]: unrecognized selector sent to instance 0x110277af0'

分配委托和委托方法等时的View Controller。

//
//  TextInputCell.swift
//  AdminApp
//
//  Created by Cooper Watts on 2016-01-27.
//  Copyright © 2016 DefineYoga. All rights reserved.

//
//  Based on the DatePickerCell.swift Created by Dylan Vann

import Foundation
import UIKit

/**
 * Text Input Cell for table views.
 */
public class TextInputCell: UITableViewCell {

    var textField: UITextField!

    /**
     Creates the TextInputCell

     - parameter style:           A constant indicating a cell style. See UITableViewCellStyle for descriptions of these constants.
     - parameter reuseIdentifier: A string used to identify the cell object if it is to be reused for drawing multiple rows of a table view. Pass nil if the cell object is not to be reused. You should use the same reuse identifier for all cells of the same form.

     - returns: An initialized TextInputCell object or nil if the object could not be created.
     */
    override public init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        setup()
    }

    private func setup() {
        // The InputField overhangs the view slightly to avoid invalid constraints.
        self.clipsToBounds = true

        textField = UITextField(frame: CGRect(x: 0, y: 15, width: self.contentView.frame.size.width - 15.0, height: self.contentView.frame.size.height))

        textField.placeholder = "PlaceHolder"

        textField.autoresizingMask = [.FlexibleRightMargin, .FlexibleLeftMargin, .FlexibleBottomMargin, .FlexibleTopMargin]
        textField.translatesAutoresizingMaskIntoConstraints = true

        self.contentView.addSubview(textField)

    }

    /**
     Needed for initialization from a storyboard.
     - parameter aDecoder: An unarchiver object.
     - returns: An initialized TextInputeCell object or nil if the object could not be created.
     */
    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }

    /**
     Update the placeholder specific to the Input Field
     - Parameter placeholder: the place holder for the input field
     */
    public func updatePlaceHolder(placeholder: String) {
        textField.placeholder = placeholder
    }

}

在viewDidLoad()中:

//Configure the Text Input Cells
var clientNameInput = TextInputCell(style: UITableViewCellStyle.Default, reuseIdentifier: nil)
var invoiceEmailInput = TextInputCell(style: UITableViewCellStyle.Default, reuseIdentifier: nil)
var invoiceAddressInput = TextInputCell(style: UITableViewCellStyle.Default, reuseIdentifier: nil)
var amountChargedInput = TextInputCell(style: UITableViewCellStyle.Default, reuseIdentifier: nil)

委托职能:

clientNameInput.textField.delegate = self
invoiceEmailInput.textField.delegate = self
invoiceAddressInput.textField.delegate = self
amountChargedInput.textField.delegate = self

clientNameInput.updatePlaceHolder(mainOptions[0])
invoiceEmailInput.updatePlaceHolder(mainOptions[1])
invoiceAddressInput.updatePlaceHolder(mainOptions[2])
amountChargedInput.updatePlaceHolder(billingOptions[0])

我将TextInputCells添加到表中,如下所示:

/*UITextField Delegate Methods*/

func textFieldDidBeginEditing(textField: UITextField) {
}
func textFieldDidEndEditing(textField: UITextField) {
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
    return true;
}
func textFieldShouldClear(textField: UITextField) -> Bool {
    return true;
}
func textFieldShouldEndEditing(textField: UITextField) -> Bool {
    return true;
}
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    return true;
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return true;
}

3 个答案:

答案 0 :(得分:1)

通过添加req.query.keys(o).forEach(function(key) { // your validations here if (key != 'amount' && key != 'long' && .... ) { // wrong param, throw error } }); 断点应该很容易看出导致此错误的原因。让应用程序崩溃,调试器将在异常即将抛出时停止。您将能够看到引发异常的确切代码行,您可以使用调试器查看应用程序的状态。 这是一个显示如何添加断点的gif: Adding an "All Exeptions" breakpoint

答案 1 :(得分:1)

OP说:

  

它停在了线上:class AppDelegate:UIResponder,   UIApplicationDelegate

AppDelegate crash

我遇到了同样的失败,因为我试图实现一种方法来处理Enter键,但我删除了与之关联的@IBAction。 ibaction  然后,当我运行应用程序时,它崩溃了。

我删除了IB动作UI中的条目(如上所示),重建并运行,一切正常。

答案 2 :(得分:0)

原来它与损坏的故事板或.xib文件有关,通过删除Tab Bar控制器并使用新配置的关系创建新控制器来解决该问题。找到了答案here