Passing arguments through a binding

  • I'm trying to find a low-maintenance way to bind the titles of my
    controls to runtime-generated (internationalized) strings, preferably in
    IB. Ideally, what I'd like to do is assign a "tag" to each control and
    bind the "title" attribute to a method so that when the control wants to
    set it's title, it calls the method passing it it's tag which then
    generates the string. I tried looking through the KVC/KVO docs and it
    looks like it is not possible to pass an argument to a method used in a
    binding, is this correct?

    My application is one of several front-ends to a shell-script based
    installer. The back-end runs on multiple platforms and provides all of
    the resources (strings) to the various front-ends upon request. It is
    not feasible to restructure this application so that the Cocoa based
    front-end uses resources from a NIB-file, they need to be provided by
    the back-end at runtime.

    If possible, I'd like to avoid having to manually query for each
    internationalized string in code. I'm a Cocoa newbie, so maybe there is
    something obvious that I'm missing, but it looks like unless I can pass
    an argument to a binding, I'm basically out of luck.

    Does anybody have any ideas?

    Thanks,
    -Nathan
  • >
    > My application is one of several front-ends to a shell-script based
    > installer. The back-end runs on multiple platforms and provides all of
    > the resources (strings) to the various front-ends upon request. It is
    > not feasible to restructure this application so that the Cocoa based
    > front-end uses resources from a NIB-file, they need to be provided by
    > the back-end at runtime.
    Obviously you  have to use bindings where titles of each of you
    controls need to be bound to a controller
    object which could be a kind of facade to your strings that  come
    from your back-end.
    >
    > If possible, I'd like to avoid having to manually query for each
    > internationalized string in code. I'm a Cocoa newbie, so maybe
    > there is
    > something obvious that I'm missing, but it looks like unless I can
    > pass
    > an argument to a binding, I'm basically out of luck.
    As I said, write a facade which hides all the details as how the
    title strings are generated. Have clean
      KVC compliant methods to return those titles. And in NIB bind your
    controls to appropriate keys
    governed by the KVC compliant methods.  Read more on KVC-KVO :

    http://developer.apple.com/documentation/Cocoa/Conceptual/
    CocoaBindings/CocoaBindings.html

    However there would be a problem where localized strings may have
    different sizes in different languages and
    you will have to take care that your controls are properly resized to
    fit the title.

    -Shripada
  • Shripad Hebbar wrote:
    >>
    >> My application is one of several front-ends to a shell-script based
    >> installer. The back-end runs on multiple platforms and provides all of
    >> the resources (strings) to the various front-ends upon request. It is
    >> not feasible to restructure this application so that the Cocoa based
    >> front-end uses resources from a NIB-file, they need to be provided by
    >> the back-end at runtime.
    > Obviously you  have to use bindings where titles of each of you
    > controls need to be bound to a controller
    > object which could be a kind of facade to your strings that  come from
    > your back-end.
    Does this mean that in my controller object I need to have a unique
    method or instance variable for each string that I want to localize? I
    was kind of hoping to find something a little less verbose.
    >>
    >> If possible, I'd like to avoid having to manually query for each
    >> internationalized string in code. I'm a Cocoa newbie, so maybe there is
    >> something obvious that I'm missing, but it looks like unless I can pass
    >> an argument to a binding, I'm basically out of luck.
    > As I said, write a facade which hides all the details as how the title
    > strings are generated. Have clean
    > KVC compliant methods to return those titles. And in NIB bind your
    > controls to appropriate keys
    > governed by the KVC compliant methods.  Read more on KVC-KVO :
    >
    > http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaBindings/Coc
    oaBindings.html

    >
    The code that generates the strings is already well abstracted, I guess
    I should have said "I'd like to avoid having to implement a unique
    method for every string". What I would like to do is bind all of my
    controls to the same key (i.e. controller.getTitle) and pass an argument
    when calling the method associated with the key, but I'm pretty sure
    this isn't possible. If there is a way to accomplish this, or some other
    clever way to accomplish my end goal, I'm all ears.
    > However there would be a problem where localized strings may have
    > different sizes in different languages and
    > you will have to take care that your controls are properly resized to
    > fit the title.
    >
    I understand this issue and will deal with it accordingly.

    Thank you for your reply and help,
    -Nathan
    > -Shripada
    >
    >
    > ------------------------------------------------------------------------
  • Le 20 déc. 07 à 15:26, Nathan Auch a écrit :

    > Shripad Hebbar wrote:
    >>>
    >>> My application is one of several front-ends to a shell-script based
    >>> installer. The back-end runs on multiple platforms and provides
    >>> all of
    >>> the resources (strings) to the various front-ends upon request. It
    >>> is
    >>> not feasible to restructure this application so that the Cocoa based
    >>> front-end uses resources from a NIB-file, they need to be provided
    >>> by
    >>> the back-end at runtime.
    >> Obviously you  have to use bindings where titles of each of you
    >> controls need to be bound to a controller
    >> object which could be a kind of facade to your strings that  come
    >> from your back-end.
    > Does this mean that in my controller object I need to have a unique
    > method or instance variable for each string that I want to localize?
    > I was kind of hoping to find something a little less verbose.
    >>>
    >>> If possible, I'd like to avoid having to manually query for each
    >>> internationalized string in code. I'm a Cocoa newbie, so maybe
    >>> there is
    >>> something obvious that I'm missing, but it looks like unless I can
    >>> pass
    >>> an argument to a binding, I'm basically out of luck.
    >> As I said, write a facade which hides all the details as how the
    >> title strings are generated. Have clean
    >> KVC compliant methods to return those titles. And in NIB bind your
    >> controls to appropriate keys
    >> governed by the KVC compliant methods.  Read more on KVC-KVO :
    >>
    >> http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaBindings/Coc
    oaBindings.html

    > The code that generates the strings is already well abstracted, I
    > guess I should have said "I'd like to avoid having to implement a
    > unique method for every string". What I would like to do is bind all
    > of my controls to the same key (i.e. controller.getTitle) and pass
    > an argument when calling the method associated with the key, but I'm
    > pretty sure this isn't possible. If there is a way to accomplish
    > this, or some other clever way to accomplish my end goal, I'm all
    > ears.
    >> However there would be a problem where localized strings may have
    >> different sizes in different languages and
    >> you will have to take care that your controls are properly resized
    >> to fit the title.
    >>
    > I understand this issue and will deal with it accordingly.
    >
    > Thank you for your reply and help,
    > -Nathan

    Have a look at -[NSObject valueForUndefinedKey:].

    http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Protoco
    ls/NSKeyValueCoding_Protocol/Reference/Reference.html


    You can override this method like this:

    - (id)valueForUndefinedKey:(NSString *)key {
    NSString *value = NSLocalizedString(key, nil, nil);
    if (!value)
      return [super valueForUndefinedKey:key];
    else
      return value;
    }
  • Am 19.12.2007 um 22:56 Uhr schrieb Nathan Auch:

    > If possible, I'd like to avoid having to manually query for each
    > internationalized string in code. I'm a Cocoa newbie, so maybe there
    > is
    > something obvious that I'm missing, but it looks like unless I can
    > pass
    > an argument to a binding, I'm basically out of luck.
    >
    > Does anybody have any ideas?

    Maybe you could subclass your controls and make them fetch their title
    in awakeFromNib (or even in initWithCoder:)?

    Andreas
  • Jean-Daniel Dupas wrote:
    >
    > Le 20 déc. 07 à 15:26, Nathan Auch a écrit :
    >
    >> Shripad Hebbar wrote:
    >>>>
    >>>> My application is one of several front-ends to a shell-script based
    >>>> installer. The back-end runs on multiple platforms and provides all of
    >>>> the resources (strings) to the various front-ends upon request. It is
    >>>> not feasible to restructure this application so that the Cocoa based
    >>>> front-end uses resources from a NIB-file, they need to be provided by
    >>>> the back-end at runtime.
    >>> Obviously you  have to use bindings where titles of each of you
    >>> controls need to be bound to a controller
    >>> object which could be a kind of facade to your strings that  come
    >>> from your back-end.
    >> Does this mean that in my controller object I need to have a unique
    >> method or instance variable for each string that I want to localize?
    >> I was kind of hoping to find something a little less verbose.
    >>>>
    >>>> If possible, I'd like to avoid having to manually query for each
    >>>> internationalized string in code. I'm a Cocoa newbie, so maybe
    >>>> there is
    >>>> something obvious that I'm missing, but it looks like unless I can
    >>>> pass
    >>>> an argument to a binding, I'm basically out of luck.
    >>> As I said, write a facade which hides all the details as how the
    >>> title strings are generated. Have clean
    >>> KVC compliant methods to return those titles. And in NIB bind your
    >>> controls to appropriate keys
    >>> governed by the KVC compliant methods.  Read more on KVC-KVO :
    >>>
    >>> http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaBindings/Coc
    oaBindings.html

    >>>
    >> The code that generates the strings is already well abstracted, I
    >> guess I should have said "I'd like to avoid having to implement a
    >> unique method for every string". What I would like to do is bind all
    >> of my controls to the same key (i.e. controller.getTitle) and pass an
    >> argument when calling the method associated with the key, but I'm
    >> pretty sure this isn't possible. If there is a way to accomplish
    >> this, or some other clever way to accomplish my end goal, I'm all ears.
    >>> However there would be a problem where localized strings may have
    >>> different sizes in different languages and
    >>> you will have to take care that your controls are properly resized
    >>> to fit the title.
    >>>
    >> I understand this issue and will deal with it accordingly.
    >>
    >> Thank you for your reply and help,
    >> -Nathan
    >
    > Have a look at -[NSObject valueForUndefinedKey:].
    >
    > http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Protoco
    ls/NSKeyValueCoding_Protocol/Reference/Reference.html

    >
    >
    > You can override this method like this:
    >
    > - (id)valueForUndefinedKey:(NSString *)key {
    > NSString *value = NSLocalizedString(key, nil, nil);
    > if (!value)
    > return [super valueForUndefinedKey:key];
    > else
    > return value;
    > }
    >
    >
    This is exactly what I was looking for. Thank you very much!

    -Nathan
previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today