是否可以通过键盘获得文本字段?

时间:2015-08-13 07:00:17

标签: ios objective-c cocoa-touch uitextfield uikit

在我的视图控制器中,我订阅了UIKeyboardWillShowNotification

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow:)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

所以在我的keyboardWillShow:方法中,我能够从通知中检索有关键盘的一些信息。但我想要的是获得引入键盘的实际文本字段的引用。我在谷歌找不到怎么做,我怀疑这可能是不可能的,但有人可能知道。如果确实不可能,那么我想知道是否有可能以相反的方式 - 通过引用文本字段来获取有关键盘的信息。谢谢

4 个答案:

答案 0 :(得分:5)

让我强调 @iphonic评论是正确的方法。

  

您应该使用UITextField委托函数textFieldShouldBeginEditing:

任何不足之处都是kludge:UIKeyboardWillShowNotification假设软件键盘会出现,这是一个非常危险的假设,在各种情况下可能会失败,从但不限于蓝牙键盘。 在模拟器中尝试cmd-K。

以上是上述kludge,灵感来自Get the current first responder without using a private API

func keyboardWillShow() {
    let firstResponder = self.findFirstResponder(inView: self.view)
    println("keyboardWillShow for \(firstResponder)")
}

func findFirstResponder(inView view: UIView) -> UIView? {
    for subView in view.subviews as! [UIView] {
        if subView.isFirstResponder() {
            return subView
        }

        if let recursiveSubView = self.findFirstResponder(inView: subView) {
            return recursiveSubView
        }
    }

    return nil
}

答案 1 :(得分:5)

有一种手动方式

MainActivity

夫特

if ([firstName isFirstResponder]) {
    // caused due to firstName
} else if ([lastName isFirstResponder]) {
    // caused due to lastName
}

答案 2 :(得分:2)

通过开始编辑Undefined symbols for architecture i386: "_deflate", referenced from: +[GAICompressionUtil gai_dataByCompressingBytes:length:compressionLevel:mode:] in libGoogleAnalyticsServices.a(GAICompressionUtil.o) "_deflateEnd", referenced from: +[GAICompressionUtil gai_dataByCompressingBytes:length:compressionLevel:mode:] in libGoogleAnalyticsServices.a(GAICompressionUtil.o) "_deflateInit2_", referenced from: +[GAICompressionUtil gai_dataByCompressingBytes:length:compressionLevel:mode:] in libGoogleAnalyticsServices.a(GAICompressionUtil.o) "_inflate", referenced from: +[GAICompressionUtil gai_dataByInflatingBytes:length:isRawData:] in libGoogleAnalyticsServices.a(GAICompressionUtil.o) "_inflateEnd", referenced from: +[GAICompressionUtil gai_dataByInflatingBytes:length:isRawData:] in libGoogleAnalyticsServices.a(GAICompressionUtil.o) "_inflateInit2_", referenced from: +[GAICompressionUtil gai_dataByInflatingBytes:length:isRawData:] in libGoogleAnalyticsServices.a(GAICompressionUtil.o) "_sqlite3_bind_blob", referenced from: -[TAGDataLayerPersistentStoreImpl writeEntriesToDatabase:expireTime:] in libGoogleAnalyticsServices.a(TAGDataLayerPersistentStoreImpl.o) "_sqlite3_bind_double", referenced from: -[GAIAnalyticsPropertiesStore bindRecord:toUpsertStatement:] in libGoogleAnalyticsServices.a(GAIAnalyticsPropertiesStore.o) "_sqlite3_bind_int", referenced from: -[TAGDataLayerPersistentStoreImpl deleteEntries:] in libGoogleAnalyticsServices.a(TAGDataLayerPersistentStoreImpl.o) "_sqlite3_bind_int64", referenced from: -[TAGDataLayerPersistentStoreImpl writeEntriesToDatabase:expireTime:] in libGoogleAnalyticsServices.a(TAGDataLayerPersistentStoreImpl.o) -[TAGDataLayerPersistentStoreImpl peekEntryIds:] in libGoogleAnalyticsServices.a(TAGDataLayerPersistentStoreImpl.o) -[TAGDataLayerPersistentStoreImpl deleteEntriesOlderThan:] in libGoogleAnalyticsServices.a(TAGDataLayerPersistentStoreImpl.o) -[GAIAnalyticsPropertiesStore bindRecord:toUpsertStatement:] in libGoogleAnalyticsServices.a(GAIAnalyticsPropertiesStore.o) "_sqlite3_bind_text", referenced from: -[GAISqlStore tableExists:] in libGoogleAnalyticsServices.a(GAISqlStore.o) -[TAGDataLayerPersistentStoreImpl clearKeysWithPrefixSingleThreaded:] in libGoogleAnalyticsServices.a(TAGDataLayerPersistentStoreImpl.o) -[TAGDataLayerPersistentStoreImpl writeEntriesToDatabase:expireTime:] in libGoogleAnalyticsServices.a(TAGDataLayerPersistentStoreImpl.o) -[GAIAnalyticsPropertiesStore bindRecord:toUpsertStatement:] in libGoogleAnalyticsServices.a(GAIAnalyticsPropertiesStore.o) -[GAIAnalyticsPropertiesStore selectStmtWithTrackingId:] in libGoogleAnalyticsServices.a(GAIAnalyticsPropertiesStore.o) "_sqlite3_close", referenced from: -[GAISqlStore closeDatabase] in libGoogleAnalyticsServices.a(GAISqlStore.o) -[TAGDataLayerPersistentStoreImpl closeDatabase] in libGoogleAnalyticsServices.a(TAGDataLayerPersistentStoreImpl.o) "_sqlite3_column_blob", referenced from: -[TAGDataLayerPersistentStoreImpl loadSerialized] in libGoogleAnalyticsServices.a(TAGDataLayerPersistentStoreImpl.o) "_sqlite3_column_bytes", referenced from: -[TAGDataLayerPersistentStoreImpl loadSerialized] in libGoogleAnalyticsServices.a(TAGDataLayerPersistentStoreImpl.o) "_sqlite3_column_count", referenced from: -[GAISqlStore openAndValidateDatabase] in libGoogleAnalyticsServices.a(GAISqlStore.o) "_sqlite3_column_double", referenced from: -[GAIAnalyticsPropertiesStore propertyRecordForTrackingId:] in libGoogleAnalyticsServices.a(GAIAnalyticsPropertiesStore.o) "_sqlite3_column_int", referenced from: -[GAISqlStore rowCountForTableWithName:] in libGoogleAnalyticsServices.a(GAISqlStore.o) "_sqlite3_column_int64", referenced from: -[TAGDataLayerPersistentStoreImpl peekEntryIds:] in libGoogleAnalyticsServices.a(TAGDataLayerPersistentStoreImpl.o) -[TAGDataLayerPersistentStoreImpl numStoredEntries] in libGoogleAnalyticsServices.a(TAGDataLayerPersistentStoreImpl.o) -[GAIAnalyticsPropertiesStore propertyRecordForTrackingId:] in libGoogleAnalyticsServices.a(GAIAnalyticsPropertiesStore.o) "_sqlite3_column_text", referenced from: -[GAISqlStore openAndValidateDatabase] in libGoogleAnalyticsServices.a(GAISqlStore.o) -[TAGDataLayerPersistentStoreImpl loadSerialized] in libGoogleAnalyticsServices.a(TAGDataLayerPersistentStoreImpl.o) -[GAIAnalyticsPropertiesStore hasValidPropertyColumns] in libGoogleAnalyticsServices.a(GAIAnalyticsPropertiesStore.o) -[GAIAnalyticsPropertiesStore propertyRecordForTrackingId:] in libGoogleAnalyticsServices.a(GAIAnalyticsPropertiesStore.o) "_sqlite3_errcode", referenced from: -[TAGDataLayerPersistentStoreImpl prepareSql:description:] in libGoogleAnalyticsServices.a(TAGDataLayerPersistentStoreImpl.o) "_sqlite3_errmsg", referenced from: -[GAISqlStore errorMessage] in libGoogleAnalyticsServices.a(GAISqlStore.o) -[TAGDataLayerPersistentStoreImpl clearKeysWithPrefixSingleThreaded:] in libGoogleAnalyticsServices.a(TAGDataLayerPersistentStoreImpl.o) -[TAGDataLayerPersistentStoreImpl writeEntriesToDatabase:expireTime:] in libGoogleAnalyticsServices.a(TAGDataLayerPersistentStoreImpl.o) -[TAGDataLayerPersistentStoreImpl deleteEntries:] in libGoogleAnalyticsServices.a(TAGDataLayerPersistentStoreImpl.o) -[TAGDataLayerPersistentStoreImpl numStoredEntries] in libGoogleAnalyticsServices.a(TAGDataLayerPersistentStoreImpl.o) -[TAGDataLayerPersistentStoreImpl deleteEntriesOlderThan:] in libGoogleAnalyticsServices.a(TAGDataLayerPersistentStoreImpl.o) -[TAGDataLayerPersistentStoreImpl tablePresent:] in libGoogleAnalyticsServices.a(TAGDataLayerPersistentStoreImpl.o) ... "_sqlite3_finalize", referenced from: -[GAISqlStore removeStatementForQuery:] in libGoogleAnalyticsServices.a(GAISqlStore.o) -[GAISqlStore finalizeAndClearStatements] in libGoogleAnalyticsServices.a(GAISqlStore.o) -[GAISqlStore openAndValidateDatabase] in libGoogleAnalyticsServices.a(GAISqlStore.o) -[GAISqlStore prepareSql:] in libGoogleAnalyticsServices.a(GAISqlStore.o) -[GAISqlStore executeSql:] in libGoogleAnalyticsServices.a(GAISqlStore.o) -[TAGDataLayerPersistentStoreImpl clearKeysWithPrefixSingleThreaded:] in libGoogleAnalyticsServices.a(TAGDataLayerPersistentStoreImpl.o) -[TAGDataLayerPersistentStoreImpl deleteEntries:] in libGoogleAnalyticsServices.a(TAGDataLayerPersistentStoreImpl.o) ... "_sqlite3_open", referenced from: -[TAGDataLayerPersistentStoreImpl openDatabaseAndInitStatements] in libGoogleAnalyticsServices.a(TAGDataLayerPersistentStoreImpl.o) "_sqlite3_open_v2", referenced from: -[GAISqlStore openAndValidateDatabase] in libGoogleAnalyticsServices.a(GAISqlStore.o) "_sqlite3_prepare_v2", referenced from: -[GAISqlStore prepareSql:] in libGoogleAnalyticsServices.a(GAISqlStore.o) -[TAGDataLayerPersistentStoreImpl prepareSql:description:] in libGoogleAnalyticsServices.a(TAGDataLayerPersistentStoreImpl.o) "_sqlite3_reset", referenced from: -[GAISqlStore rowCountForTableWithName:] in libGoogleAnalyticsServices.a(GAISqlStore.o) -[GAISqlStore tableExists:] in libGoogleAnalyticsServices.a(GAISqlStore.o) -[GAISqlStore dropTableWithName:] in libGoogleAnalyticsServices.a(GAISqlStore.o) -[TAGDataLayerPersistentStoreImpl loadSerialized] in libGoogleAnalyticsServices.a(TAGDataLayerPersistentStoreImpl.o) -[TAGDataLayerPersistentStoreImpl writeEntriesToDatabase:expireTime:] in libGoogleAnalyticsServices.a(TAGDataLayerPersistentStoreImpl.o) -[TAGDataLayerPersistentStoreImpl peekEntryIds:] in libGoogleAnalyticsServices.a(TAGDataLayerPersistentStoreImpl.o) -[TAGDataLayerPersistentStoreImpl numStoredEntries] in libGoogleAnalyticsServices.a(TAGDataLayerPersistentStoreImpl.o) ... "_sqlite3_step", referenced from: -[GAISqlStore rowCountForTableWithName:] in libGoogleAnalyticsServices.a(GAISqlStore.o) -[GAISqlStore openAndValidateDatabase] in libGoogleAnalyticsServices.a(GAISqlStore.o) -[GAISqlStore tableExists:] in libGoogleAnalyticsServices.a(GAISqlStore.o) -[GAISqlStore dropTableWithName:] in libGoogleAnalyticsServices.a(GAISqlStore.o) -[GAISqlStore executeSql:] in libGoogleAnalyticsServices.a(GAISqlStore.o) -[TAGDataLayerPersistentStoreImpl clearKeysWithPrefixSingleThreaded:] in libGoogleAnalyticsServices.a(TAGDataLayerPersistentStoreImpl.o) -[TAGDataLayerPersistentStoreImpl loadSerialized] in libGoogleAnalyticsServices.a(TAGDataLayerPersistentStoreImpl.o) ... ld: symbol(s) not found for architecture i386 clang: error: linker command failed with exit code 1 (use -v to see invocation) UITextField的通知,有更好的方法可以做到这一点:

UITextView

UITextFieldTextDidBeginEditingNotification

UITextViewTextDidBeginEditingNotification

答案 3 :(得分:1)

最简单(也是几乎唯一)的方法也是最好的方法(如@iphonic所述)

  

实施UITextFieldDelegate委托方法textFieldShouldBeginEditing:textField

在任何其他事件之前(包括所有键盘通知),该方法将被称为

  

存储在确定哪个字段触发此事件时要引用的UITextField引用。


我还建议您执行以下操作:如果要确定是哪个键盘触发了当前的键盘外观,请内部使用此委托方法简单地注册以接收键盘通知。

- (BOOL)textFieldShouldBeginEditing:(UITextView *)textView {

    [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

    return YES;

}

这样,您可以确定在调用keyboardWillShow:时,它实际上是从此特定文本字段触发的。

当然,要确保正确跟踪文本字段,请在文本字段停止编辑时停止收听。

- (void)textFieldDidEndEditing:(UITextView *)textView {

    [NSNotificationCenter.defaultCenter removeObserver:self name:UIKeyboardWillShowNotification object:nil];

}