Disable/Enable NSButton if NSTextfield is empty or not

后端 未结 4 2121
清歌不尽
清歌不尽 2021-02-08 01:05

I´m newbie with cocoa. I have a button and a textField in my app. I want the button disabled when the textfield is empty and enabled when the user type something.

Any po

4条回答
  •  逝去的感伤
    2021-02-08 01:47

    Thanks Peter. What I missed (in my hard way version) is this piece of code in the awakeFromNib in the appDelegate:

    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(textDidChange:) name:NSControlTextDidChangeNotification object:myTextField];
    

    It works perfect. Now I´m trying the easy way, but I´m afraid I´m not still good enough with the bindings.

    To bind the property

    @property (retain) IBOutlet NSString *aStringValue;
    

    with the textfield´s value, what I have to use in IB for "Bind to:", "Controller Key" and "Model Key Path"?

提交回复
热议问题