从文本字段更新属性边界,而不需要按Enter键 [英] Update property bound from text field without needing to press Enter

查看:136
本文介绍了从文本字段更新属性边界,而不需要按Enter键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本字段,并将其绑定到 NSString 实例变量。

I have a text field and I bind it to an NSString instance variable.

文本字段,它不更新变量。它等待,直到我按Enter键。我不想每次都按Enter键。

When I type in the text field, it does not update the variable. It waits until I press the Enter key. I don't want to hit Enter every time.

我需要更改什么才能立即更改绑定值?

What do I need to change in order to make the binding change value immediately?

推荐答案

默认情况下, NSTextField 的值绑定不会连续更新。要解决这个问题,您需要在选择文本字段后,在值标题下检查绑定检查器中的持续更新值框:

By default, the value binding of an NSTextField does not update continuously. To fix this, you need, after selecting your text field, to check the "Continuously Updates Value" box in the Bindings Inspector under the Value heading:

,通常,你真正想做的是更新文本字段绑定的属性,当用户完成编辑并按下按钮(保存或OK)。要做到这一点,你不需要不断更新属性,如上所述,你只需要结束编辑。 Daniel Jalkut提供了一个非常有用的实施方法: / p>

However, most often, what you really want to do is update the property to which the text field is bound when the user has finished editing and presses a button ("Save" or "OK", for example). To do this, you needn't continuously update the property as described above, you just need to end editing. Daniel Jalkut provides an extremely useful implementation of just such a method:

@interface NSWindow (Editing)

- (void)endEditing;

@end

@implementation NSWindow (Editing)

- (void)endEditing
{
    // Save the current first responder, respecting the fact
    // that it might conceptually be the delegate of the 
    // field editor that is "first responder."
    id oldFirstResponder = [oMainDocumentWindow firstResponder];
    if ((oldFirstResponder != nil) &&
        [oldFirstResponder isKindOfClass:[NSTextView class]] &&
        [(NSTextView*)oldFirstResponder isFieldEditor])
    {   
        // A field editor's delegate is the view we're editing
        oldFirstResponder = [oldFirstResponder delegate];
        if ([oldFirstResponder isKindOfClass:[NSResponder class]] == NO)
        {
            // Eh ... we'd better back off if 
            // this thing isn't a responder at all
            oldFirstResponder = nil;
        }
    } 

    // Gracefully end all editing in our window (from Erik Buck).
    // This will cause the user's changes to be committed.
    if([oMainDocumentWindow makeFirstResponder:oMainDocumentWindow])
    {
        // All editing is now ended and delegate messages sent etc.
    }
    else
    {
        // For some reason the text object being edited will
        // not resign first responder status so force an 
        /// end to editing anyway
        [oMainDocumentWindow endEditingFor:nil];
    }

    // If we had a first responder before, restore it
    if (oldFirstResponder != nil)
    {
        [oMainDocumentWindow makeFirstResponder:oldFirstResponder];
    }
}

@end



如果你有一个保存按钮定位到你的视图控制器的方法 -save:,你会调用

- (IBAction)save:(id)sender
{
    [[[self view] window] endEditing];
    //at this point, all properties bound to text fields have the same
    //value as the contents of the text fields.

    //save stuff...
}

这篇关于从文本字段更新属性边界,而不需要按Enter键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆